【问题标题】:Pandas total count each day熊猫每天的总数
【发布时间】: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

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    试试这个:

    df = df.groupby(['datetime','id','col3']).count()
    

    【讨论】:

      【解决方案2】:

      如果您希望所有列的计数值仅基于日期,那么:

      df.groupby('datetime').count()
      

      您将获得一个 DataFrame,它以日期时间为索引,列单元格表示该给定索引的条目数。

      【讨论】:

        猜你喜欢
        • 2013-07-16
        • 1970-01-01
        • 2020-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        相关资源
        最近更新 更多