这是上一个问题的后续内容:Plot number of occurrences from Pandas DataFrame
我正在尝试按照“发布Office”分组的pandas数据帧的结果以降序生成条形图。数据来自csv文件,该文件有3列:System(字符串),Issuing Office(字符串),错误类型(字符串)。前四个命令工作正常 - 读取,修复列标题,删除我不需要的办公室,并重置索引。但是我以前从未展示过图表。
CSV看起来像:
System Issuing Office Error Type
East N1 Error1
East N1 Error1
East N2 Error1
West N1 Error3
寻找一个简单的水平条形图,显示N1的计数为3,N2的计数为2.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('mydatafile.csv',index_col=None, header=0) #ok
df.columns = [c.replace(' ','_') for c in df.columns] #ok
df = df[df['Issuing_Office'].str.contains("^(?:N|M|V|R)")] #ok
df = df.reset_index(drop=True) #ok
# produce chart that shows how many times an office came up (Decending)
df.groupby([df.index, 'Issuing_Office']).count().plot(kind='bar')
plt.show()
# produce chart that shows how many error types per Issuing Office (Descending).
此处没有日期字段,使其与原始问题不同。非常感谢任何帮助:)
答案 0 :(得分:1)
JohnE的解决方案奏效了。使用代码:
# produce chart that shows how many times an office came up (Decending)
df['Issuing_Office'].value_counts().plot(kind='barh') #--JohnE
plt.gca().invert_yaxis()
plt.show()
# produce chart that shows how many error types per Issuing Office N1 (Descending).
dfN1 = df[df['Issuing_Office'].str.contains('N1')]
dfN1['Error_Type'].value_counts().plot(kind='barh')
plt.gca().invert_yaxis()
plt.show()