使用 GPS 坐标进行字典操作



我有一个巨大的数据,有两个id,开始和结束时间以及GPS坐标。我正在尝试根据GPS坐标查找频率,总和,平均值,中位数,最大值,最小值,接触持续时间以及频率。

004096f41eb8 00904bf131ad 40.0 820219 438869 820219 438869
004096f41eb8 00904bf469bd 40.0 820219 438869 820219 438869
00022d56dffe 00022dcbe817 962.0 820353 439280 820353 439280
00022dcbe817 00306511e9e0 540.0 820353 439280 820353 439280
00022dcbe817 00904b21787a 4250.0 820353 439280 820353 439280
00022dcbe817 00904b3b845a 1117.0 820353 439280 820353 439280
00022dcbe817 00904bc3be80 1117.0 820353 439280 820353 439280
00022dcbe817 00904bcd5f00 4250.0 820353 439280 820353 439280
00022dcbe817 00904bfebc7c 3737.0 820353 439280 820353 439280

以上是输入示例。 col[0]col[1]是 ID。当涉及到ids时,我可以很好地找到频率,总和,平均值,中位数,最大值,最小值,接触持续时间。但是当我必须同时考虑 Id 和 GPS 坐标时,我无法将其包含在下面给出的相同代码中。我需要找到具有相同 GPS 坐标的 id 的频率。

例如: col[0] col[1] col[3] col[4] col

[5] col[6]

from collections import defaultdict
import numpy as np
pairtimelist = defaultdict(list)
pairgpslist = defaultdict(list)
with open('input', 'r') as f, open('output_all', 'w') as o_one, open('output_contact', 'w') as o_two, open('output_gps', 'w') as o_three:
    for numline, line in enumerate((line.split() for line in f), start=1):
        pair = line[0], line[1]
        pairtimelist[pair].append(float(line[2]))
        pairgps = line[0], line[1], line[3], line[4], line[5], line[6]
        #pairgpslist[pairgps].append(float(line[2])) 
    for pair in pairtimelist.iterkeys():
        timeavg = np.mean(pairtimelist[pair])
        timemed = np.median(pairtimelist[pair])
        timesum = np.sum(pairtimelist[pair])
        timemax = np.max(pairtimelist[pair])
        timemin = np.min(pairtimelist[pair])
        freq = len(pairtimelist[pair])
    for pairgps in pairgpslist.iterkeys():
        freqgps = len(pairgpslist[pairgps]
    #print pair, pairtimelist[pair]
    o_two.write("%s %s n" % (pair, pairtimelist[pair]))
    o_one.write("%s %s %s %.2f %.2f %s %s %s n" % (pair[0], pair[1], freq, timesum, timeavg, timemed, timemax, timemin))
    o_three.write("%s %s n" % (pair, freqgps))
Error: 
 File "pair_gps.py", line 21
    o_two.write("%s %s n" % (pair, pairtimelist[pair]))
        ^
SyntaxError: invalid syntax

但是,当注释所有相关的GPS时,不会看到此错误。他们有什么简单的方法可以在一起吗?提前谢谢。

在你的第 19 行:

freqgps = len(pairgpslist[pairgps]

您缺少 len 函数的结束括号。

顺便说一下,你可以非常轻松地实现你想要做的事情,并且使用熊猫以一种不太容易出错的方式。它将是这样的:

import pandas as pd
import numpy as np
pd.read_csv('input.csv')
  .groupby([0, 1])[2]
  .agg([np.mean, np.median, np.sum, np.max, np.min])
  .reset_index()
  .to_csv('output.csv')

最新更新