【发布时间】:2017-03-27 06:57:36
【问题描述】:
这是上一个问题的后续:Plot number of occurrences from Pandas DataFrame
我正在尝试从按“发行办公室”分组的 pandas 数据框的结果按降序生成条形图。数据来自一个 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).
其中没有日期字段,这使得它与原始问题不同。非常感谢任何帮助:)
【问题讨论】:
-
也许这就是你想要的?
df['issuing_office'].value_counts().plot(kind='bar')我认为您根本不需要在这里使用 groupby,或者如果您这样做了,可能不是要在其中包含索引?我的意思是,如果你只是从“groupby”中删除“df.index”,它基本上可以工作,尽管有一些无关的东西。
标签: python-3.x pandas matplotlib charts