1

Using the groupby method

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

I got the following table: table

The initial table had the this king of structure:

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

The problem

I want to build barplots for each connection name depicting distribution of features between two Alarm Types.

What I get:

res

The desired output:

out

My last code for building plot only for one connection name:

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)

Also, the problem is, that the script builds multiple plots by date (I interrupted kernel on the 77th plot) with the identical output.

What am I doing wrong? unstacking (unstack().plot.barh()) also didn't help.

Any clues are welcome.

Community
  • 1
  • 1
Ison
  • 403
  • 2
  • 9
  • You can provide a [mcve], also see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples); else I don't see how one can give a useful answer here. – ImportanceOfBeingErnest Sep 04 '19 at 14:30

1 Answers1

2

It looks like you are trying to produce a plot for each (date, conName). Here's what I would do:

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)

Output will be several plots like this, where 2 is the conName:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • that makes sense. but for some reason any time i'm trying to group by alarm in loop I get the error "Empty 'DataFrame': no numeric data to plot", the origin of which i do not completely understand. – Ison Sep 04 '19 at 14:12
  • Probably because one combination of `date, conName` has all 0 values, so when you mask/count, it gives you empty frame. – Quang Hoang Sep 04 '19 at 14:14
  • yep, found the reason. some features have values, but the Alarm is not provided. so basically it does grouping by alarm, but the resulf is empty df. thanks for help – Ison Sep 04 '19 at 14:42