2016-11-13 3 views
-1

これは、前の質問からフォローです:私はによってグループ化されたパンダのデータフレームの結果から、降順で棒グラフを作成しようとしているPlot number of occurrences from Pandas DataFrameプロット数(2)

"発行オフィス"。データは、System(ストリング)、発行オフィス(ストリング)、エラータイプ(ストリング)の3つの列を持つcsvファイルから得られます。最初の4つのコマンドは正常に動作します。読み込み、列ヘッダーの修正、必要のないオフィスの削除、インデックスのリセットを行います。しかし、私は前にチャートを表示したことはありません。 CSVがどのように見える

:N1は3の数を有し示すだろう、単純な横棒グラフをお探し

System Issuing Office Error Type 
East N1    Error1 
East N1    Error1 
East N2    Error1 
West N1    Error3 

、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

多分これはあなたが欲しいものですか?あなたがここで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() 
関連する問題