python - 使用 Python 创建用户轨迹

标签 python sorting gps gis

我需要一些帮助来在 python 中创建轨迹。我有一个包含 user_ids、时间戳、纬度和经度的 CSV。我想在我的文件中创建第五列(或一起写入新文件),其中包含每个 user_id 的轨迹,这需要按用户对坐标进行分组,并按时间戳排序(升序)。我能够在 PostGIS 中完成,但结果是几何图形,而我需要的是坐标字符串。我尝试过使用集合和 itertools 以及 Pandas 进行分组和排序,但我很挣扎。

作为引用,这里是我的几行数据:

user_id, timestamp, latitude, longitude
478134225, 3/12/2017 9:04, 38.8940974, -77.0276216
478103585, 3/12/2017 9:04, 38.882584, -77.1124701
478073193, 3/12/2017 9:07, 39.00027849, -77.09480086
476194185, 3/12/2017 9:14, 38.8048355, -77.0469214
476162349, 3/12/2017 9:16, 38.8940974, -77.0276216
478073193, 3/12/2017 9:05, 38.8549, -76.8752
477899275, 3/12/2017 9:08, 38.90181532, -77.03733586
477452890, 3/12/2017 9:08, 38.96117237, -76.95561893
478073193, 3/12/2017 9:05, 38.7188716, -77.1542684

如您所见,有些用户具有多个条目,因此我需要按它们进行分组并按时间对坐标进行排序。例如。类似于下面的内容

478073193 (38.8549,-76.8752)(38.7188716,-77.1542684)(39.00027849,-77.09480086)

我有超过 150k 点,所以像上面那样手动操作不是一个选择。

最佳答案

这应该可以做到。它使用流行的 pandas 包和 numpy。

import pandas as pd
import numpy as np

# First we import the data
data = pd.read_csv('data.csv', delimiter=', ')

# Now get a list of unique user_id values
uniqueIds = np.unique(data['user_id'].values)

# Now just get the ordered (by timestamp) coordinates for each user_id
output = [[id,data.loc[data['user_id']==id].sort_values(by='timestamp')[['latitude','longitude']].values.tolist()] for id in uniqueIds]

输出是一个列表,其中每个元素的形式为 [id, list_of_paired_coordinates],例如:

[[476162349, [[38.8940974, -77.02762159999999]]],
 [478073193, [[38.8549, -76.8752],
              [38.7188716, -77.1542684],
              [39.00027849, -77.09480086]]],
 [478103585, [[38.882584, -77.11247009999998]]],
 [478134225, [[38.8940974, -77.02762159999999]]]]

编辑

如果您想以 JSON 格式保存输出,您可以执行以下操作:

import json

# Now turn our array into a dict {id:coordinates}
outputDict = {}
for i in output:
    outputDict[i[0]]=i[1]

with open('output.json', 'w') as f:
    json.dump(outputDict, f, sort_keys=True, indent=4, ensure_ascii=False, separators=(',',':'))

根据您想要的输出文件格式,您可能需要稍微修改 outputDict 的结构(如果是这种情况,请告诉我)。现在 JSON 文件如下所示:

{
    "476162349":[
        [
            38.8940974,
            -77.02762159999999
        ]
    ],
    "476194185":[
        [
            38.804835499999996,
            -77.0469214
        ]
    ....

编辑2

如果要格式化输出 JSON 文件,您可以向存储在字典中的值添加标签:

outputDict = {}
for i in output:
    coords={}
    latList=[]
    longList=[]
    for j in i[1]:
    latList.append(j[0])
    longList.append(j[1])
    coords["latitude"]=latList
    coords["longitude"]=longList
    outputDict[i[0]]=coords

输出 JSON 文件将如下所示:

{
    "476162349":{
        "latitude":[
            38.8940974
        ],
        "longitude":[
            -77.02762159999999
        ]
    },
    "476194185":{
        "latitude":[
            38.804835499999996
        ],
        "longitude":[
            -77.0469214
        ]
    }
        ....

关于python - 使用 Python 创建用户轨迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43130907/

相关文章:

python - 在使用此多对多关系之前,django "<User: user556>"需要为字段 "id"提供一个值

保存相关变量的 Pythonic 方式?

javascript - 使用 jQuery 对表格行进行排序时忽略空单元格

java - 如何利用速度和时间计算下一次的经纬度

c# - 如何通过 C# 中的 Selenium 在 ChromeDriver 中伪造/模拟地理位置?

Python——读取文件并确定最低分数和人

python - 如何使用 ANN 和遗传算法在 Python 中为井字游戏创建 AI?

vba - 如何从最新到最旧对列进行排序?

java - java中的错误排序字母列表

windows-phone-8 - 如何将经纬度值转换为Windows Phone中的地址?