【发布时间】:2020-11-17 14:25:08
【问题描述】:
我有一个包含很多列的大型数据集 (df),我正在尝试获取每天的总数。
|datetime|id|col3|col4|col...
1 |11-11-2020|7|col3|col4|col...
2 |10-11-2020|5|col3|col4|col...
3 |09-11-2020|5|col3|col4|col...
4 |10-11-2020|4|col3|col4|col...
5 |10-11-2020|4|col3|col4|col...
6 |07-11-2020|4|col3|col4|col...
我希望我的结果是这样的
|datetime|id|col3|col4|col...|Count
6 |07-11-2020|4|col3|col4|col...| 1
3 |5|col3|col4|col...| 1
2 |10-11-2020|5|col3|col4|col...| 1
4 |4|col3|col4|col...| 2
1 |11-11-2020|7|col3|col4|col...| 1
我尝试像df = df.groupby(['id','col3', pd.Grouper(key='datetime', freq='D')]).sum().reset_index() 这样使用重新采样,这就是我的结果。我对编程和 Pandas 还很陌生,但我已经阅读了 pandas 文档,但仍然无法做到。
|datetime|id|col3|col4|col...
6 |07-11-2020|4|col3|1|0.0
3 |07-11-2020|5|col3|1|0.0
2 |10-11-2020|5|col3|1|0.0
4 |10-11-2020|4|col3|2|0.0
1 |11-11-2020|7|col3|1|0.0
【问题讨论】: