【问题标题】:How to count by time frequency using groupby - pandas如何使用 groupby - pandas 按时间频率计数
【发布时间】:2020-02-07 18:38:53
【问题描述】:

我正在尝试使用我的df 中的 2 列按月计算 2 个事件的频率。到目前为止,我所做的是按唯一时间计算所有事件,这不够有效,因为结果太多。我希望之后创建一个带有结果的图表。

我尝试通过 SO 问题的答案来调整我的代码:

但当我在groupby 命令中输入freq='day' 时,该命令似乎无法正常工作。

我的代码是:

print(df.groupby(['Priority', 'Create Time']).Priority.count())

最初产生类似 170000 的结果的结构如下:

Priority  Create Time        
1.0       2011-01-01 00:00:00    1
          2011-01-01 00:01:11    1
          2011-01-01 00:02:10    1
                  ...

2.0       2011-01-01 00:01:25    1
          2011-01-01 00:01:35    1
                  ...

但现在由于某种原因(我使用的是 Jupyter Notebook)它只产生:

Priority  Create Time        
1.0       2011-01-01 00:00:00    1
          2011-01-01 00:01:11    1
          2011-01-01 00:02:10    1
2.0       2011-01-01 00:01:25    1
          2011-01-01 00:01:35    1
Name: Priority, dtype: int64

不知道为什么输出变成了只有 5 个结果(也许我在不知不觉中改变了一些东西)。

我希望结果采用以下格式:

Priority  month     Count     
1.0       2011-01     a
          2011-02     b
          2011-03     c
                ...

2.0       2011-01     x
          2011-02     y
          2011-03     z
                ...

显示如何正确更改其他值的频率的要点,例如hour/day/month/year。有了答案,请您解释一下您的代码中发生了什么,因为我是新手,正在学习熊猫并希望了解该过程。谢谢。

【问题讨论】:

    标签: python pandas pandas-groupby


    【解决方案1】:

    一种可能的解决方案是通过Series.dt.to_period 将日期时间列转换为月份:

    print(df.groupby(['Priority', df['Create Time'].dt.to_period('m')]).Priority.count())
    

    或者使用Grouper:

    print(df.groupby(['Priority', pd.Grouper(key='Create Time', freq='MS')]).Priority.count())
    

    示例

    np.random.seed(123)
    
    df = pd.DataFrame({'Create Time':pd.date_range('2019-01-01', freq='10D', periods=10),
                       'Priority':np.random.choice([0,1], size=10)})
    
    print (df)
      Create Time  Priority
    0  2019-01-01         0
    1  2019-01-11         1
    2  2019-01-21         0
    3  2019-01-31         0
    4  2019-02-10         0
    5  2019-02-20         0
    6  2019-03-02         0
    7  2019-03-12         1
    8  2019-03-22         1
    9  2019-04-01         0
    

    print(df.groupby(['Priority', df['Create Time'].dt.to_period('m')]).Priority.count())
    Priority  Create Time
    0         2019-01        3
              2019-02        2
              2019-03        1
              2019-04        1
    1         2019-01        1
              2019-03        2
    Name: Priority, dtype: int64
    
    print(df.groupby(['Priority', pd.Grouper(key='Create Time', freq='MS')]).Priority.count())
    Priority  Create Time
    0         2019-01-01     3
              2019-02-01     2
              2019-03-01     1
              2019-04-01     1
    1         2019-01-01     1
              2019-03-01     2
    Name: Priority, dtype: int64
    

    【讨论】:

    • 第一个我得到AttributeError: 'RangeIndex' object has no attribute 'to_period',第二个只产生2个结果
    • 好的 - 第一个使用 .dt.to_period('m')。不过,我仍然只得到 2 个结果 - 我期待更多。你知道如何应对吗?
    • @pragmaticlearner - 嗯,日期时间和你预期的一样吗?因为这似乎是一些与数据相关的问题。
    • 我想是这样...我在导入 csv 文件时的命令是df = pd.read_csv('records-for-2011.csv', parse_dates=['Create Time'])。它以前可以使用 170000+ 行,但是当我开始调整它以获得我想要的东西时,它以某种方式改变了。不知道为什么
    • @pragmaticlearner - 代码看起来不错,是否可以检查日期时间是否有更多月份,例如 2?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多