1 回答
TA贡献1934条经验 获得超2个赞
df2 = df.pivot(columns='flow').resample('5T').sum().stack()
df2.index = df2.index.swaplevel(0, 1)
>>> df2.sort_index()
cars
flow time
In 2020-10-20 09:05:00 737.0
2020-10-20 09:10:00 0.0
2020-10-20 09:15:00 42.0
Out 2020-10-20 09:05:00 22.0
2020-10-20 09:10:00 0.0
2020-10-20 09:15:00 0.0
Unknown 2020-10-20 09:05:00 0.0
2020-10-20 09:10:00 0.0
2020-10-20 09:15:00 36.0
我个人会将数据保留为表格形式:
>>> df.pivot(columns='flow').resample('5T').sum()
cars
flow In Out Unknown
time
2020-10-20 09:05:00 737.0 22.0 0.0
2020-10-20 09:10:00 0.0 0.0 0.0
2020-10-20 09:15:00 42.0 0.0 36.0
添加回答
举报