这必须是最简单的事情,我只是想不通。
我有一个这样的数据框:
station ts EventType
0 BLOOR 2020-02-04 20:35:00 Valid Entry
1 BLOOR 2020-02-07 17:45:00 Valid Exit
2 COLLEGE 2020-02-05 14:20:00 Valid Exit
3 BAY 2020-02-04 16:55:00 Valid Entry
4 UNION 2020-02-07 08:20:00 Valid Exit
我只想要一个数据框,用于拆分“ EventType”列,并按站点和ts对它们进行计数:
station ts EntryCount ExitCount
0 BLOOR 2020-02-04 20:35:00 5 2
1 BLOOR 2020-02-04 20:40:00 10 6
2 COLLEGE 2020-02-04 20:35:00 3 7
3 COLLEGE 2020-02-04 20:40:00 6 8
4 COLLEGE 2020-02-04 20:45:00 5 9
我没有成功地沿着这些路径走下去:
df.pivot_table(index=['station','ts'], columns="EventType", values="EventType", aggfunc=sum)
要么
df['EntryCount'] = df.groupby(["ts", "station"], as_index=False, sort=False)["EventType"].apply(
lambda etype: etype == "Valid Entry"
).sum()
(值得一提的是-数据框也很大)
怎么样:
df.groupby(['station','ts']).EventType.value_counts().unstack()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句