重新采样后,我希望唯一值成为新的列标题并将其计数作为值。
我尝试过
new_df.set_index('time').resample('60T').apply(lambda x: list(x))
time camera_id
0 2019-05-06 12:00:00 [cam02]
1 2019-05-06 13:00:00 []
2 2019-05-06 14:00:00 [cam01, cam01, cam01, cam02, cam02, cam02, cam...
3 2019-05-06 15:00:00 [cam02, cam02, cam02, cam02, cam02, cam02, cam...
我想要这样
time cam01 cam02 cam03
0 2019-05-06 12:00:00 0 1 0
1 2019-05-06 13:00:00 0 0 0
2 2019-05-06 14:00:00 0 10 0
3 2019-05-06 15:00:00 1 5 1
答案 0 :(得分:1)
我相信您需要在resample
和DataFrame.groupby
之前Resampler.size
,并通过Series.unstack
重塑:
df = (new_df.set_index('time')
.groupby('camera_id')
.resample('60T')
.size()
.unstack(0, fill_value=0))
print (df)
camera_id cam01 cam02 cam03
time
2019-05-06 12:00:00 0 1 0
2019-05-06 13:00:00 0 0 0
2019-05-06 14:00:00 0 10 0
2019-05-06 15:00:00 1 5 1