Issue
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
Solution
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
Answered By - Vishnudev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.