【问题标题】:summing the number of occurrences per day pandas总结每天熊猫的出现次数
【发布时间】:2013-07-16 09:06:04
【问题描述】:

我在 pandas 数据框中有这样的数据集:

                                  score
timestamp                                 
2013-06-29 00:52:28+00:00        -0.420070
2013-06-29 00:51:53+00:00        -0.445720
2013-06-28 16:40:43+00:00         0.508161
2013-06-28 15:10:30+00:00         0.921474
2013-06-28 15:10:17+00:00         0.876710

我需要计算发生的测量次数,所以我正在寻找这样的东西:

                                    count
   timestamp
   2013-06-29                       2
   2013-06-28                       3

我不关心情绪列,我想要每天发生的次数。

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

如果您的timestamp 索引是DatetimeIndex

import io
import pandas as pd
content = '''\
timestamp  score
2013-06-29 00:52:28+00:00        -0.420070
2013-06-29 00:51:53+00:00        -0.445720
2013-06-28 16:40:43+00:00         0.508161
2013-06-28 15:10:30+00:00         0.921474
2013-06-28 15:10:17+00:00         0.876710
'''

df = pd.read_table(io.BytesIO(content), sep='\s{2,}', parse_dates=[0], index_col=[0])

print(df)

所以df 看起来像这样:

                        score
timestamp                    
2013-06-29 00:52:28 -0.420070
2013-06-29 00:51:53 -0.445720
2013-06-28 16:40:43  0.508161
2013-06-28 15:10:30  0.921474
2013-06-28 15:10:17  0.876710

print(df.index)
# <class 'pandas.tseries.index.DatetimeIndex'>

你可以使用:

print(df.groupby(df.index.date).count())

产生

            score
2013-06-28      3
2013-06-29      2

注意parse_dates 参数的重要性。没有它,索引将只是一个pandas.core.index.Index 对象。在这种情况下,您不能使用df.index.date

所以答案取决于你没有显示的type(df.index)...

【讨论】:

  • 如果我的索引数据类型是 Int64Index 怎么办?我已经使用 pd.to_datetime(df["end_time"].astype('str'), format='%Y-%m-%d %H:%M:%S') 更改了它,所以当我执行 df .dtypes 它正确返回 datetime 数据类型,但是当我将 end_time 列设置为索引并打印索引时,它返回 Int64 数据类型。
  • 补充这个答案...将 datetime64[ns] 转换为 DatetimeIndex 请参阅:stackoverflow.com/questions/59690099/… DatetimeIndex 文档:pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.html
【解决方案2】:

否则,使用resample 函数。

In [419]: df
Out[419]: 
timestamp
2013-06-29 00:52:28   -0.420070
2013-06-29 00:51:53   -0.445720
2013-06-28 16:40:43    0.508161
2013-06-28 15:10:30    0.921474
2013-06-28 15:10:17    0.876710
Name: score, dtype: float64

In [420]: df.resample('D', how={'score':'count'})

Out[420]: 
2013-06-28    3
2013-06-29    2
dtype: int64

更新:熊猫 0.18+

正如@jbochi 指出的那样,现在不推荐使用how 重新采样。改用:

df.resample('D').apply({'score':'count'})

【讨论】:

  • 现在不推荐使用how 重新采样。你应该使用df.resample('D').apply({'score':'count'})
【解决方案3】:
In [145]: df
Out[145]: 
timestamp
2013-06-29 00:52:28   -0.420070
2013-06-29 00:51:53   -0.445720
2013-06-28 16:40:43    0.508161
2013-06-28 15:10:30    0.921474
2013-06-28 15:10:17    0.876710
Name: score, dtype: float64

In [160]: df.groupby(lambda x: x.date).count()
Out[160]: 
2013-06-28    3
2013-06-29    2
dtype: int64

【讨论】:

  • 嗯。你知道为什么df.index[0].date 返回&lt;function date&gt;吗?
  • 嗯。我不。 @安迪?
  • 好吧,date.index.date 是索引上的一个属性,它的类型为 DatetimeIndex,而 index[0] 已经只是一个时间戳,它不提供日期属性,但链接到一个方法时间戳。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2018-09-22
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2022-06-23
  • 2019-01-16
相关资源
最近更新 更多