绘制每个日期的出现次数 [英] Plotting the count of occurrences per date

查看:78
本文介绍了绘制每个日期的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对熊猫数据框非常陌生,它具有一个日期时间列和一个包含文本字符串(标题)的列。每个标题将是一个新行。

I'm very new to pandas data frame that has a date time column, and a column that contains a string of text (headlines). Each headline will be a new row.

我需要在x轴上绘制日期,而y轴需要包含标题出现在多少次上每个日期。

I need to plot the date on the x-axis, and the y-axis needs to contain how many times a headline occurs on each date.

因此,例如,一个日期可能包含3个标题。

So for example, one date may contain 3 headlines.

最简单的方法是这个?我根本不知道该怎么做。也许添加另一列,每行带有 1?如果是这样,您将如何做?

What's the simplest way to do this? I can't figure out how to do it at all. Maybe add another column with a '1' for each row? If so, how would you do this?

请向我指出任何可能有帮助的方向!

Please point me in the direction of anything that may help!

谢谢!

我尝试在y上绘制计数,但一直出现错误,我尝试创建一个用于计算行数的变量,但这没有

I have tried plotting the count on the y, but keep getting errors, I tried creating a variable that counts the number of rows, but that didn't return anything of use either.

我尝试添加带有标题计数的列

I tried add a column with the count of headlines

df_data['headline_count'] = df_data['headlines'].count

,然后我尝试了方法

df_data['count'] = df.groupby('headlines')['headlines'].transform('count')

使用Groupie时,出现错误

When I use groupie, i get an error of

KeyError: 'headlines'

输出应该只是一个图,该图重复数据帧中的日期重复了多少次(这表明存在多个y轴上的行)。并且x轴应该是观察到的日期。

The output should simply be a plot with how many times a date is repeated in the dataframe (which signals that there are multiple headlines) in the rows plotted on the y-axis. And the x-axis should be the date that the observations occurred.

推荐答案

尝试一下:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

A = pd.DataFrame(columns=["Date", "Headlines"], data=[["01/03/2018","Cricket"],["01/03/2018","Football"],
                                                    ["02/03/2018","Football"],["01/03/2018","Football"],
                                                    ["02/03/2018","Cricket"],["02/03/2018","Cricket"]] )

您的数据如下所示:

print (A)

       Date Headlines
0   01/03/2018  Cricket
1   01/03/2018  Football
2   02/03/2018  Football
3   01/03/2018  Football
4   02/03/2018  Cricket
5   02/03/2018  Cricket

现在通过操作对其进行分组:

Now do a group by operation on it:

data = A.groupby(["Date","Headlines"]).size()
print(data)

Date        Headlines
01/03/2018  Cricket      1
            Football     2
02/03/2018  Cricket      2
            Football     1
dtype: int64

现在您可以绘图它使用以下代码:

You can now plot it using the below code:

# set width of bar
barWidth = 0.25

# set height of bar
bars1 = data.loc[(data.index.get_level_values('Headlines') =="Cricket")].values
bars2 = data.loc[(data.index.get_level_values('Headlines') =="Football")].values


# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]

# Make the plot
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='Cricket')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='Football')

# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], data.index.get_level_values('Date').unique())

# Create legend & Show graphic
plt.legend()
plt.xlabel("Date")
plt.ylabel("Count")
plt.show()

希望这会有所帮助!

这篇关于绘制每个日期的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆