【发布时间】:2020-01-07 09:53:52
【问题描述】:
使用 groupby 方法
df = Al[['Connection Name','UAS-RS', 'SES-RS', 'OFS-RS', 'ES-RS', 'BBE-RS', 'Date', 'Alarm Type']]
cols = ['Connection Name', 'Date', 'Alarm Type']
df.mask(df == 0, np.nan).groupby(cols)[['UAS-RS', 'SES-RS', 'OFS-RS', 'ES-RS', 'BBE-RS']].count()
我得到了下表:
初始表有这个结构之王:
| ConName | Date | Alarm | ETH1 | ETH2 | ETH 3|
| AR21 | 25-01-19 | AL1 | 1 | 0 | 3 |
| AR22 | 25-01-19 | AL2 | 0 | 0 | 1 |
| AR23 | 26-01-19 | AL1 | 1 | 1 | 0 |
| AR21 | 26-01-19 | AL2 | 0 | 1 | 0 |
问题
我想为每个连接名称构建条形图,描述两种警报类型之间的特征分布。
我得到了什么:
想要的输出:
我最后一个只为一个连接名称构建绘图的代码:
for date in df[df['Connection Name']=='AR-CY18 A2.5G-RTA33']['Date']:
df.mask(df == 0, np.nan).groupby('Alarm Type')[['UAS-RS', 'SES-RS', 'OFS-RS', 'ES-RS', 'BBE-RS']].count().plot(kind='bar',stacked=True,subplots=True)
另外,问题是,该脚本按日期构建了多个绘图(我在第 77 个绘图上中断了内核),并具有相同的输出。
我做错了什么? unstacking (unstack().plot.barh()) 也没有帮助。
欢迎提供任何线索。
【问题讨论】:
-
可以提供minimal reproducible example,另见How to make good reproducible pandas examples;否则我看不出如何在这里给出有用的答案。
标签: python pandas matplotlib charts