如何根据时间戳划分pandas数据帧?

时间:2017-11-05 16:05:33

标签: python pandas dataframe

我有一个pandas数据框列时间戳,ID,纬度,经度。这个数据框大约有一个月的时间。如何绘制不同的访问位置VS时间(每天或每周)的数量?

Time Stamp              Id  Latitude    Longitude
08/10/2016 15:22:51:700 1      23        50
08/10/2016 16:28:08:026 1      23        50
08/10/2016 16:28:09:026 1      12        45
08/10/2016 19:00:08:026 2      23        50
08/10/2016 20:28:08:026 1      23        50
08/10/2016 19:00:08:000 2      23        50
09/10/2016 01:02:33:123 2      23        50
09/10/2016 06:15:08:500 1      23        50
09/10/2016 10:01:07:022 3      28        88

1 个答案:

答案 0 :(得分:1)

我相信你需要:

首先按to_datetime创建日期时间Series - times已删除,因此请暂时不要datetime。 (感谢cᴏʟᴅsᴘᴇᴇᴅ的想法)

然后groupby并获得nunique,最后plot

days = pd.to_datetime(df['Time Stamp'].str.split().str[0])

s1 = df['Id'].groupby(days).nunique()
print (s1)
Time Stamp
2016-08-10    2
2016-09-10    3
Name: Id, dtype: int64


s1.plot()

几周转换为week s:

weeks = days.dt.week

s2 = df['Id'].groupby(weeks).nunique()
print (s2)
Time Stamp
32    2
36    3
Name: Id, dtype: int64

s2.plot()

所有日期的另一种方法是resample

df['Days'] = pd.to_datetime(df['Time Stamp'].str.split().str[0])

s2 = df.resample('D', on='Days')['Id'].nunique()
print (s2)
Days
2016-08-10    2
2016-08-11    0
2016-08-12    0
2016-08-13    0
2016-08-14    0
2016-08-15    0
2016-08-16    0
2016-08-17    0
2016-08-18    0
2016-08-19    0
2016-08-20    0
2016-08-21    0
2016-08-22    0
2016-08-23    0
2016-08-24    0
2016-08-25    0
2016-08-26    0
2016-08-27    0
2016-08-28    0
2016-08-29    0
2016-08-30    0
2016-08-31    0
2016-09-01    0
2016-09-02    0
2016-09-03    0
2016-09-04    0
2016-09-05    0
2016-09-06    0
2016-09-07    0
2016-09-08    0
2016-09-09    0
2016-09-10    3
Freq: D, Name: Id, dtype: int64
s2.plot()

几周:

s2 = df.resample('W', on='Days')['Id'].nunique()
print (s2)
Days
2016-08-14    2
2016-08-21    0
2016-08-28    0
2016-09-04    0
2016-09-11    3
Freq: W-SUN, Name: Id, dtype: int64


s2.plot()