Shown below are the details from a DataFrame
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
How can I add a percentage column shown below,
CodePudding user response:
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('%')
)