我认为你可以使用 value_counts
和 sort_index
,最后使用 Series.plot.bar
生成条形图:
weatherDFConcat['TEMPBIN_CON'].value_counts().sort_index().plot.bar()
或者如果你想使用你自己的解决方案:
Temp_Counts = Counter(weatherDFConcat['TEMPBIN_CON'])
df = pd.DataFrame.from_dict(Temp_Counts, orient = 'index').sort_index()
df.plot(kind = 'bar')
样例:
import matplotlib.pyplot as plt
weatherDFConcat = pd.DataFrame({'TEMPBIN_CON':['30 degrees or more', '-15 to -18 degrees', '-3 to 0 degrees', '15 to 18 degrees', '-15 to -18 degrees'
]})
print (weatherDFConcat)
TEMPBIN_CON
0 30 degrees or more
1 -15 to -18 degrees
2 -3 to 0 degrees
3 15 to 18 degrees
4 -15 to -18 degrees
weatherDFConcat['TEMPBIN_CON'].value_counts().sort_index().plot.bar()
plt.show()
TEMPBIN_CON
拆分为两个: LOWER_TEMP
和HIGHER_TEMP
。然后,您可以使用以下方式按这些列对DataFrame进行排序:sorted = df.sort_values(by=['LOWER_TEMP', 'HIGHER_TEMP'])
然后您按照之前的方式进行绘图:
sorted.plot(kind='bar')