1

使用 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()) 也没有帮助。

欢迎提供任何线索。

4

1 回答 1

2

看起来您正在尝试为每个(date, conName). 这是我要做的:

for (dt, con), d in df.groupby(['Date','ConName'], group_keys=False):
    fig,ax = plt.subplots()
    d.groupby('Alarm')[['ETH1','ETH2']].count().plot.bar(ax=ax)
    ax.set_title(con)

输出将是几个这样的图,2其中conName

在此处输入图像描述

于 2019-09-04T13:53:27.230 回答