By AJG519


2015-06-20 00:15:53 8 Comments

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.

1 comments

@abeboparebop 2015-06-20 00:25:16

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

Some other options are discussed here.

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Add one row to pandas DataFrame

7 Answered Questions

[SOLVED] Writing a pandas DataFrame to CSV file

20 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

19 Answered Questions

[SOLVED] Get list from pandas DataFrame column headers

15 Answered Questions

[SOLVED] Selecting multiple columns in a pandas dataframe

15 Answered Questions

[SOLVED] "Large data" work flows using pandas

5 Answered Questions

[SOLVED] Creating an empty Pandas DataFrame, then filling it?

13 Answered Questions

[SOLVED] Delete column from pandas DataFrame

23 Answered Questions

[SOLVED] Adding new column to existing DataFrame in Python pandas

10 Answered Questions

[SOLVED] How to select rows from a DataFrame based on column values?

Sponsored Content