在 pandas 中绘制系列直方图 [英] Plotting series histogram in Pandas

查看:90
本文介绍了在 pandas 中绘制系列直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Pandas中绘制一系列直方图,如下所示:

I am plotting a series histogram in Pandas as follows:

df['Primary Type'].value_counts().plot(kind='bar')

尽管如此,该系列有25个唯一值,并且该图绘制许多条形图.可以将频率较低的条形仅组合成一个吗?

Nevertheless, this series have 25 unique values and the plot draws many bars. Is it possible to group the bars with lower frequency in only one?

谢谢.

推荐答案

您可以通过

You can do it by filtering by boolean indexing:

np.random.seed(100)
df = pd.DataFrame(np.random.randint(20, size=(20,1)), columns=['Primary Type'])
print (df)
    Primary Type
0              8
1              3
2              7
3             15
4             16
5             10
6              2
7              2
8              2
9             14
10             2
11            17
12            16
13            15
14             4
15            11
16            16
17             9
18             2
19            12


s = df['Primary Type'].value_counts()
print (s)
2     5
16    3
15    2
17    1
14    1
12    1
11    1
10    1
9     1
8     1
7     1
4     1
3     1
Name: Primary Type, dtype: int64

#all values under trash sum to one category
tresh = 2
a = s[s <= tresh].sum()
s = s[s > tresh]
#then add to filtered df
s.loc['another'] = a
print (s)
2           5
16          3
another    12
Name: Primary Type, dtype: int64

#last plot
s.plot(kind='bar')

这篇关于在 pandas 中绘制系列直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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