How to get the a percentage column along with the value/ count column

Question:

Shown below are the details from a DataFrame

enter image description here

Below is the Syntax used to add a percentage column,

df1 = df[['Attrition', 'Gender',"JobSatisfaction"]]
df1 = df1.groupby(['Attrition','Gender'])['Job_Satisfaction'].value_counts().reset_index(name='count')
df1['%'] =   100 * df1['count']/  df1.groupby(['Attrition','Gender','Job_Satisfaction'])['count'].transform('sum')
df1 = df1 .sort_values(by=['Gender','Attrition','Job_Satisfaction'])
df1 

below is the results I get

enter image description here

How can I add a percentage column shown below,
enter image description here

Asked By: WF30

||

Answers:

You can normalize using groupby.transform('sum') and multiply by 100:

df['%'] = df['count'].div(df.groupby('Gender')['count'].transform('sum')).mul(100)

For a string:

df['%'] = (df['count']
           .div(df.groupby('Gender')['count'].transform('sum')
           .mul(100).astype(int).astype(str).add('%')
           )
Answered By: mozway

The percentage denominator you want is total Gender count, hence df1.groupby(['Attrition','Gender','Job_Satisfaction']) was incorrect.
Use df1.groupby(['Gender']) instead.

Answered By: Marc Rives
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.