2016-11-13 77 views
-1

這是從以前的問題進行後續:Plot number of occurrences from Pandas DataFrame地塊編號(2)

我想產生一個柱狀圖中從由一個分組數據框大熊貓的結果降序排列「發佈辦公室」。數據來自一個csv文件,它有3列:系統(字符串),頒發辦公室(字符串),錯誤類型(字符串)。前四個命令可以正常工作 - 讀取,修復列標題,去掉不需要的辦公室,然後重置索引。不過,我從來沒有顯示過圖表。

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). 

計數有沒有日期這使得它與原始問題不同。任何幫助非常感謝:)

+1

也許這就是你想要的? 'df ['issuing_office'] .value_counts()。plot(kind ='bar')'我不認爲你需要在這裏使用groupby,或者如果你這樣做,可能並不意味着包含索引它?我的意思是,如果你只是從「groupby」中刪除「df.index」,它基本上會工作,儘管有一些無關的東西。 – JohnE

回答

0

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()