Calculating percent of 'group' total without creating new pandas dataframe

Question:

I’m trying to add a new series to a dataframe that calculates the percent of group total. Thing is, I don’t want a new dataframe. Here is my sample data:

df:

enter image description here

my desired output would be:

enter image description here

Any ideas, thanks in advance!

Asked By: Mike Mann

||

Answers:

Following BigBen’s suggestion, you can calculate this as:

df['Percent total of male'] = df.groupby('Gender')['Amount'].transform(lambda x: x/x.sum())

Otherwise, in a more step-by-step approach you can also try:

df['Percent total of male'] = np.where(df['Gender'] == 'Male',df['Amount'] / df[df['Gender'] =='Male']['Amount'].sum(),df['Amount'] / df[df['Gender']!='Male']['Amount'].sum()

In which df['Amount'].sum() will be a scalar, and the division will calculate each rows value as % of total.

Answered By: Celius Stingher
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.