Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按日计算_Python_Pandas - Fatal编程技术网

Python 按日计算

Python 按日计算,python,pandas,Python,Pandas,我有一个数据框,其中包含在处创建的列,以及看起来像这样的实体 created_at entities 2017-10-29 23:06:28 {'hashtags': [{'text': 'OPEC', 'indices': [0, ... 2017-10-29 22:28:20 {'hashtags': [{'text': 'Iraq', 'indices': [21,... 2017-10-29 20:01:37

我有一个数据框,其中包含在处创建的列
,以及看起来像这样的
实体

    created_at                         entities
2017-10-29 23:06:28     {'hashtags': [{'text': 'OPEC', 'indices': [0, ...
2017-10-29 22:28:20     {'hashtags': [{'text': 'Iraq', 'indices': [21,...
2017-10-29 20:01:37     {'hashtags': [{'text': 'oil', 'indices': [58, ...
2017-10-29 20:00:14     {'hashtags': [{'text': 'oil', 'indices': [38, ...
2017-10-27 08:44:30     {'hashtags': [{'text': 'Iran', 'indices': [19,...
2017-10-27 08:44:10     {'hashtags': [{'text': 'Oil', 'indices': [17, ...
2017-10-27 08:43:13     {'hashtags': [{'text': 'Oil', 'indices': [0, 4...
2017-10-27 08:43:00     {'hashtags': [{'text': 'Iran', 'indices': [19,.
我想计算每天实体的数量。基本上我想收到类似的东西

created_at    number_of_entities
2017-10-29           4
2017-10-27           4

怎么做?我有
pandas 0.23.4

您可以使用
df.groupby(df.created\u at.dt.day)
按天分组

至于计算计数的函数,我们需要一整行,您的数据结构看起来非常奇怪。

>>> df
           created_at  entities
0 2017-10-29 23:06:28         1
1 2017-10-29 22:28:20         2
2 2017-10-29 20:01:37         3
3 2017-10-29 20:00:14         4
4 2017-10-27 08:44:30         5
5 2017-10-27 08:44:10         6
6 2017-10-27 08:43:13         7
7 2017-10-27 08:43:00         8

您可以发布:

>>> pd.PeriodIndex(df['created_at'], freq='D').value_counts()
2017-10-29    4
2017-10-27    4
Freq: D, Name: created_at, dtype: int64
jezrael在评论中建议了一种没有
周期索引
构造函数的更好方法:

>>> df['created_at'].dt.to_period('D').value_counts()
2017-10-27    4
2017-10-29    4
通过一些额外的重命名来匹配您的输出,它开始看起来像耶兹雷尔的解决方案

或者,您可以将索引设置为日期,然后重新采样:

>>> df.set_index('created_at').resample('D').size()
created_at
2017-10-27    4
2017-10-28    0
2017-10-29    4
Freq: D, dtype: int64
。。。如果需要转换为精确输出:

>>> resampled = df.set_index('created_at').resample('D').size()
>>> resampled[resampled != 0].reset_index().rename(columns={0: 'number_of_entities'})
  created_at  number_of_entities
0 2017-10-27                   4
1 2017-10-29                   4

更多内容:
resample
对于任意时间间隔特别有用,例如“五分钟”。下面的示例直接取自Wes McKinney的书“Python for Data Analysis”


使用
groupby.size

# Convert to datetime dtype if you haven't.
df1.created_at = pd.to_datetime(df1.created_at)

df2 = df1.groupby(df1.created_at.dt.date).size().reset_index(name='number_of_entities')

print (df2)

   created_at  number_of_entities
0  2017-10-27                   4
1  2017-10-29                   4
根据您的数据:

In [3]: df
Out[3]: 
            created_at                                           entities
0  2017-10-29 23:06:28  {'hashtags': [{'text': 'OPEC', 'indices': [0, ...
1  2017-10-29 22:28:20  {'hashtags': [{'text': 'Iraq', 'indices': [21,...
2  2017-10-29 20:01:37  {'hashtags': [{'text': 'oil', 'indices': [58, ...
3  2017-10-29 20:00:14  {'hashtags': [{'text': 'oil', 'indices': [38, ...
4  2017-10-27 08:44:30  {'hashtags': [{'text': 'Iran', 'indices': [19,...
5  2017-10-27 08:44:10  {'hashtags': [{'text': 'Oil', 'indices': [17, ...
6  2017-10-27 08:43:13  {'hashtags': [{'text': 'Oil', 'indices': [0, 4...
7  2017-10-27 08:43:00    {'hashtags': [{'text': 'Iran', 'indices': [19,.
您可以使用以下方法获得您想要的:

In [4]: df[["created_at"]].groupby(pd.to_datetime(df["created_at"]).dt.date).count().rename(columns={"created_at":"number_of_entities"}).reset_index()
    ...: 
Out[4]: 
   created_at  number_of_entities
0  2017-10-27                   4
1  2017-10-29                   4
注意:

如果在
列中创建的
已经是datetime格式,您可以简单地使用以下内容:

df[["created_at"]].groupby(df.created_at.dt.date).count().rename(columns={"created_at":"number_of_entities"}).reset_index()
您可以将或用于删除时间,然后用于计数、最后一列和两列
DataFrame

df = (df['created_at'].dt.floor('d')
                     .value_counts()
                     .rename_axis('created_at')
                     .reset_index(name='number_of_entities'))
print (df)
  created_at  number_of_entities
0 2017-10-29                   4
1 2017-10-27                   4
或:

如果要避免在
值\u计数中进行默认排序
传递参数
sort=False

df = (df['created_at'].dt.floor('d')
                     .value_counts(sort=False)
                     .rename_axis('created_at')
                     .reset_index(name='number_of_entities'))

你能给出一个数据帧,它有更多的行,这样输出就有不同的分组吗?这将是一个更有用的示例,谢谢。@timgeb添加了示例
periodIndex
和使用
groupby
之间的区别是什么?@lapots您可以使用
groupby
获得与其他答案相同的输出。使用
PeriodIndex
或重采样只是使用专门为处理时段而设计的
pandas
工具。在你的情况下,我们处理的是天数。@lapots-默认情况下,按计数排序时存在差异,
groupby+size/count
notWell,你可以随时将
sort=False
传递给
value\u counts
。@jezrael嘿,我还不是你这样的高手,所以不要对非传统的方法感到惊讶:D
dt。to_period
很酷,我在中编辑了这个。
df[["created_at"]].groupby(df.created_at.dt.date).count().rename(columns={"created_at":"number_of_entities"}).reset_index()
df = (df['created_at'].dt.floor('d')
                     .value_counts()
                     .rename_axis('created_at')
                     .reset_index(name='number_of_entities'))
print (df)
  created_at  number_of_entities
0 2017-10-29                   4
1 2017-10-27                   4
df = (df['created_at'].dt.date
                     .value_counts()
                     .rename_axis('created_at')
                     .reset_index(name='number_of_entities'))
df = (df['created_at'].dt.floor('d')
                     .value_counts(sort=False)
                     .rename_axis('created_at')
                     .reset_index(name='number_of_entities'))