Reputation: 253
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,
Upvotes: 1
Views: 305
Reputation: 1
The percentage denominator you want is total Gender count, hence df1.groupby(['Attrition','Gender','Job_Satisfaction'])
was incorrect.
Use df1.groupby(['Gender'])
instead.
Upvotes: 0
Reputation: 260420
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('%')
)
Upvotes: 2