gpt4 book ai didi

pandas - 如何从 Pandas 系列绘制条形图?

转载 作者:行者123 更新时间:2023-12-03 22:59:45 24 4
gpt4 key购买 nike

考虑我的系列如下:第一列是 article_id,第二列是频率计数。

article_id  
1 39
2 49
3 187
4 159
5 158
...
16947 14
16948 7
16976 2
16977 1
16978 1
16980 1

Name: article_id, dtype: int64

我使用以下命令从数据框中获得了这个系列:
logs.loc[logs['article_id'] <= 17029].groupby('article_id')['article_id'].count()

logs 是这里的数据框,而 article_id 是其中的列之一。

如何绘制条形图(使用 Matlplotlib),使 article_id 在 X 轴上,频率计数在 Y 轴上?

我的本能是使用 .tolist() 将其转换为列表,但这不会保留 article_id。

最佳答案

您需要的IIUC Series.plot.bar :

#pandas 0.17.0 and above
s.plot.bar()
#pandas below 0.17.0
s.plot('bar')

样本:
import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series({16976: 2, 1: 39, 2: 49, 3: 187, 4: 159,
5: 158, 16947: 14, 16977: 1, 16948: 7, 16978: 1, 16980: 1},
name='article_id')
print (s)
1 39
2 49
3 187
4 159
5 158
16947 14
16948 7
16976 2
16977 1
16978 1
16980 1
Name: article_id, dtype: int64


s.plot.bar()

plt.show()

graph

关于pandas - 如何从 Pandas 系列绘制条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37514686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com