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

Time:10-06

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

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('%')
           )
  • Related