Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to add group totals to a dataframe in Pandas

I have a simple task that I'm wondering if there is a better / more efficient way to do. I have a dataframe that looks like this:

  Group  Score  Count
0     A      5    100
1     A      1     50
2     A      3      5
3     B      1     40
4     B      2     20
5     B      1     60

And I want to add a column that holds the value of the group total count:

  Group  Score  Count  TotalCount
0     A      5    100         155
1     A      1     50         155
2     A      3      5         155
3     B      1     40         120
4     B      2     20         120
5     B      1     60         120

The way I did this was:

Grouped=df.groupby('Group')['Count'].sum().reset_index()
Grouped=Grouped.rename(columns={'Count':'TotalCount'})

df=pd.merge(df, Grouped, on='Group', how='left')

Is there a better / cleaner way to add these values directly to the dataframe?

Thanks for the help.

like image 931
AJG519 Avatar asked Jun 20 '15 00:06

AJG519


People also ask

How do I add totals to Groupby pandas?

To get the sum (or total) of each group, you can directly apply the pandas sum() function to the selected columns from the result of pandas groupby. The following is a step-by-step guide of what you need to do. Group the dataframe on the column(s) you want. Select the field(s) for which you want to estimate the sum.

How do I sum multiple columns in pandas DataFrame?

Use DataFrame. groupby(). sum() to group rows based on one or multiple columns and calculate sum agg function. groupby() function returns a DataFrameGroupBy object which contains an aggregate function sum() to calculate a sum of a given column for each group.

How do you add a total to a data frame?

To get the total or sum of a column use sum() method, and to add the result of the sum as a row to the DataFrame use loc[] , at[] , append() and pandas. Series() methods.

How do you count groups in pandas?

You can use pandas DataFrame. groupby(). count() to group columns and compute the count or size aggregate, this calculates a rows count for each group combination.

How to group by sum in pandas Dataframe?

Groupby sum in pandas dataframe python Groupby sum in pandas python can be accomplished by groupby() function. Groupby sum of multiple column and single column in pandas is accomplished by multiple ways some among them are groupby() function and aggregate() function.

How to add group level mean as new column in pandas?

How To Add Group Level Mean as New Column with Pandas map () function ? Another way to add group-level mean as a new column is to use Pandas map () function and dictionary. We first apply groupby and get group-level summary statistics, either mean or median. Then convert the summary dataframe to a dictionary.

How to group by multiple columns in pandas python using agg ()?

Using aggregate () function: agg () function takes ‘sum’ as input which performs groupby sum, reset_index () assigns the new index to the grouped by dataframe and makes them a proper dataframe structure 1 ''' Groupby multiple columns in pandas python using agg ()'''

How do I know if the result is a pandas Dataframe?

The output now appears in the format that we wanted. Note that the name argument within reset_index () specifies the name for the new column produced by GroupBy. We can also confirm that the result is indeed a pandas DataFrame: #display object type of df_out type(df_out) pandas.core.frame.DataFrame


1 Answers

df['TotalCount'] = df.groupby('Group')['Count'].transform('sum')

Some other options are discussed here.

like image 59
abeboparebop Avatar answered Oct 13 '22 00:10

abeboparebop