william007
william007

Reputation: 18545

Drawing bar charts from boolean fields:

I have three boolean fields, where their count is shown below:

enter image description here

I want to draw a bar chart that have

Offline_RetentionByTime with 37528

Offline_RetentionByCount with 29640

Offline_RetentionByCapacity with 3362

How to achieve that?

Upvotes: 4

Views: 7993

Answers (1)

jezrael
jezrael

Reputation: 863166

I think you can use apply value_counts for creating new df1 and then DataFrame.plot.bar:

df = pd.DataFrame({'Offline_RetentionByTime':[True,False,True, False],
                   'Offline_RetentionByCount':[True,False,False,True],
                   'Offline_RetentionByCapacity':[True,True,True, False]})

print (df)
  Offline_RetentionByCapacity Offline_RetentionByCount Offline_RetentionByTime
0                        True                     True                    True
1                        True                    False                   False
2                        True                    False                    True
3                       False                     True                   False

df1 = df.apply(pd.value_counts)
print (df1)
       Offline_RetentionByCapacity  Offline_RetentionByCount  \
True                             3                         2   
False                            1                         2   

       Offline_RetentionByTime  
True                         2  
False                        2  

df1.plot.bar()

graph

If need plot only True values select by loc:

df1.loc[True].plot.bar()

graph

Upvotes: 4

Related Questions