I want to get a percentage of a particular value in a df column. Say I have a df with (col1, col2 , col3, gender) gender column has values of M or F. I want to get the percentage of M and F values in the df.
I have tried this, which gives me the number M and F instances, but I want these as a percentage of the total number of values in the df.
df.groupby('gender').size()
Can someone help?
finding the percentage of target variation to chenck imbalance/not.
counts percentage
0 36548 88.734583
1 4640 11.265417
finding the maximum in the columns percentage here, to check how much #imbalance there
If you do not need to look
M
andF
values other thangender
column then, may be you can try usingvalue_counts()
andcount()
as following:Result:
Or, using
groupby
:Use
value_counts
withnormalize=True
:Let's say there are 200 values out of which 120 are categorized as M and 80 as F
1)
2)
3)