Python - 计算日期范围内的唯一标签
Python - Counting Unique Labels in a Date Range
我正在尝试对从 Internet 上抓取的一堆文本数据执行情绪分析。我已经达到了我的 Pandas DataFrame 具有以下我希望分析的列的程度:"post_date"(格式为 dd-mm-yyyy,即 01-10-2017)和 "Sentiment" (格式为 "positive"、"neutral" 或 "negative")。
我希望能够统计每个 day/month/year 的帖子数以及每天 positive/neutral/negative 的帖子数。
例如:
print pd.value_counts(df.Sentiment)
然而我被卡住了,我已经尝试了 groupby 命令的多次迭代(如下),但不断出现错误。
df.groupby(df.post_date.dt.year)
任何人都可以帮助我如何实现这一目标吗?
理想情况下,所需的输出类似于:
Date, Postive_Posts, Negative_Posts, Neutral_Posts, Total_Posts
01/10/2017, 10, 5, 8, 23
02/10/2017, 5, 20, 5, 30
日期是信息的分组方式(日、月、年等),pos/neg/neu 列是对应于该范围内标签数的帖子总数,最后 total_posts 是该范围内的帖子总数。
当前数据为:
post_date, Sentiment
19/09/2017, positive
19/09/2017, positive
19/09/2017, positive
20/09/2017, negative
20/09/2017, neutral
如果您需要更多信息,请告诉我。
您可以使用 groupby
+ size
+ unstack
+ add_suffix
+ sum
:
df1 = df.groupby(['post_date','Sentiment']).size().unstack(fill_value=0).add_suffix('_Posts')
df1['Total_Posts'] = df1.sum(axis=1)
print (df1)
Sentiment negative_Posts neutral_Posts positive_Posts Total_Posts
post_date
19/09/2017 0 0 3 3
20/09/2017 1 1 0 2
一行解决方案非常相似——只需要assign
:
df1 = (df.groupby(['post_date','Sentiment'])
.size()
.unstack(fill_value=0)
.add_suffix('_Posts')
.assign(Total_Posts=lambda x: x.sum(axis=1)))
print (df1)
Sentiment negative_Posts neutral_Posts positive_Posts Total_Posts
post_date
19/09/2017 0 0 3 3
20/09/2017 1 1 0 2
对于来自 index
的列:
df1 = (df.groupby(['post_date','Sentiment'])
.size()
.unstack(fill_value=0)
.add_suffix('_Posts')
.assign(Total_Posts=lambda x: x.sum(axis=1))
.reset_index()
.rename_axis(None, axis=1))
print (df1)
post_date negative_Posts neutral_Posts positive_Posts Total_Posts
0 19/09/2017 0 0 3 3
1 20/09/2017 1 1 0 2
我正在尝试对从 Internet 上抓取的一堆文本数据执行情绪分析。我已经达到了我的 Pandas DataFrame 具有以下我希望分析的列的程度:"post_date"(格式为 dd-mm-yyyy,即 01-10-2017)和 "Sentiment" (格式为 "positive"、"neutral" 或 "negative")。
我希望能够统计每个 day/month/year 的帖子数以及每天 positive/neutral/negative 的帖子数。
例如:
print pd.value_counts(df.Sentiment)
然而我被卡住了,我已经尝试了 groupby 命令的多次迭代(如下),但不断出现错误。
df.groupby(df.post_date.dt.year)
任何人都可以帮助我如何实现这一目标吗?
理想情况下,所需的输出类似于:
Date, Postive_Posts, Negative_Posts, Neutral_Posts, Total_Posts
01/10/2017, 10, 5, 8, 23
02/10/2017, 5, 20, 5, 30
日期是信息的分组方式(日、月、年等),pos/neg/neu 列是对应于该范围内标签数的帖子总数,最后 total_posts 是该范围内的帖子总数。
当前数据为:
post_date, Sentiment
19/09/2017, positive
19/09/2017, positive
19/09/2017, positive
20/09/2017, negative
20/09/2017, neutral
如果您需要更多信息,请告诉我。
您可以使用 groupby
+ size
+ unstack
+ add_suffix
+ sum
:
df1 = df.groupby(['post_date','Sentiment']).size().unstack(fill_value=0).add_suffix('_Posts')
df1['Total_Posts'] = df1.sum(axis=1)
print (df1)
Sentiment negative_Posts neutral_Posts positive_Posts Total_Posts
post_date
19/09/2017 0 0 3 3
20/09/2017 1 1 0 2
一行解决方案非常相似——只需要assign
:
df1 = (df.groupby(['post_date','Sentiment'])
.size()
.unstack(fill_value=0)
.add_suffix('_Posts')
.assign(Total_Posts=lambda x: x.sum(axis=1)))
print (df1)
Sentiment negative_Posts neutral_Posts positive_Posts Total_Posts
post_date
19/09/2017 0 0 3 3
20/09/2017 1 1 0 2
对于来自 index
的列:
df1 = (df.groupby(['post_date','Sentiment'])
.size()
.unstack(fill_value=0)
.add_suffix('_Posts')
.assign(Total_Posts=lambda x: x.sum(axis=1))
.reset_index()
.rename_axis(None, axis=1))
print (df1)
post_date negative_Posts neutral_Posts positive_Posts Total_Posts
0 19/09/2017 0 0 3 3
1 20/09/2017 1 1 0 2