alex3465
alex3465

Reputation: 419

Percentage plot from pandas based on two coulmns

Data

 List=['X','Y'] 

ColA  ColB  ColC   

te     Y    a
te     Y    a
alo         a
te     Y    b
te     2    b
bb     Y    b
aa     X    c
alo    u    c
aa     b    c

I want to plot a bar diagram with col A on x-axis and percentage of values having any value from list in the col B for example for te (3/4)*100, aa(1/1)*100, alo(0/1)*100 and so on

Upvotes: 1

Views: 62

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

A one-liner can be

df.groupby('ColA').ColB.apply(lambda x: x.notna().sum()/len(x)).mul(100).plot(kind='bar')

Data provided

  ColA  ColB
0   te     Y
1   te     Y
2  alo  None
3   te     Y
4   te  None
5   bb     Y
6   aa     X

Note: Don't forget to import matplotlib

from matplotlib import pyplot as plt

Output

enter image description here

Upvotes: 1

Related Questions