我正在Pandas中绘制一系列直方图,如下所示:
df['Primary Type'].value_counts().plot(kind='bar')
然而,这个系列有25个独特的值,情节画了很多条。是否可以将频率较低的条形分组只有一个?
提前谢谢。
答案 0 :(得分:3)
答案 1 :(得分:2)
您可以filtering
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')