Python, extracting daily data from monthly data list that has multiple entries per day

时间:2018-01-15 18:21:05

标签: python python-3.x

I have a data file with satellite measurements of global fire activity, which looks like this: YYYYMMDD, Latitude and Longitude (where lat and long correspond to the location of fire detection).

YYYYMMDD    lat     lon
20170301    -18.509 144.51
20170301    -21.018 145.06
20170301    -22.894 150.48
20170302    -24.077 151.01
20170302    -26.023 151.295

It's one month worth of data and has multiple detections per day. What I am trying to do is make a plot for each day of the month.

My question is how do I loop through all the days and then for each day extract all the lats and longs so I can plot them ... and then do it again for the next day and so on 31 times?

P.S. My YYMMDD column is now separated into year, month and day, so I am only looking at the days.

1 个答案:

答案 0 :(得分:0)

您可以创建一个字典,其中日期为键,坐标元组列表为值:

from collections import defaultdict
out = defaultdict(list)

with open('file.txt') as f:
    next(f)
    for line in f:
        line = line.split()
        out[line[0]].append((line[1], line[2]))

从您的示例数据中,您将获得:

In [18]: dict(out)
Out[18]: 
{'20170301': [('-18.509', '144.51'),
  ('-21.018', '145.06'),
  ('-22.894', '150.48')],
 '20170302': [('-24.077', '151.01'), ('-26.023', '151.295')]}