2

I'm trying to group this dataframe by "Name" and "Site", and I want to make 4 new columns that find the sum, count groupby's, average and standard deviation of the "Spend" column.

Here is my code so far:

import pandas as pd

df=pd.DataFrame({'Name':['Harry','John','Holly','John','John','John','Holly','Holly','Molly','Molly','Holly','Harry','Harry','Harry'], 'Spend': [76,43,23,43,234,54,34,12,43,54,65,23,12,32],
                 'Site': ['Amazon','Ikea','Apple','Amazon', 'Apple', 'Ikea', 'Apple', 'Apple', 'Amazon', 'Amazon', 'Ikea', 'Amazon', 'Amazon', 'Ikea']})

print (df)

Currently my dataframe looks like this:

enter image description here

And I want it to look like this:

enter image description here

How would I go about doing this?

Thanks in advance

EDIT 10/11/18:

Code:

import pandas as pd

df=pd.DataFrame({'Name':['Harry','John','Holly','John','John','John','Holly','Holly','Molly','Molly','Holly','Harry','Harry','Harry'], 'Spend': [76,43,23,43,234,54,34,12,43,54,65,23,12,32],
                 'Site': ['Amazon','Ikea','Apple','Amazon', 'Apple', 'Ikea', 'Apple', 'Apple', 'Amazon', 'Amazon', 'Ikea', 'Amazon', 'Amazon', 'Ikea'], 'Spend2': [176,143,123,143,1234,154,134,112,143,254,365,423,512,632]})

print (df)

Before:

enter image description here

After:

enter image description here

semiflex
  • 1,176
  • 3
  • 25
  • 44
  • Possible duplicate of [Apply multiple functions to multiple groupby columns](https://stackoverflow.com/questions/14529838/apply-multiple-functions-to-multiple-groupby-columns) – Dani Mesejo Nov 09 '18 at 23:11

1 Answers1

5
df_summary = df.groupby(['Name', 'Site']).agg([np.sum, pd.Series.count, np.mean, np.std])
df_summary.columns = ['Sum', 'Count Groupbys', 'Average', 'Standard Deviation']
df_summary = df_summary.reset_index().sort_values(['Site', 'Name'])

>>> df_summary
    Name    Site  Sum  Count Groupbys  Average  Standard Deviation
0  Harry  Amazon  111               3     37.0           34.219877
4   John  Amazon   43               1     43.0                 NaN
7  Molly  Amazon   97               2     48.5            7.778175
2  Holly   Apple   69               3     23.0           11.000000
5   John   Apple  234               1    234.0                 NaN
1  Harry    Ikea   32               1     32.0                 NaN
3  Holly    Ikea   65               1     65.0                 NaN
6   John    Ikea   97               2     48.5            7.778175

Per your edit, you can use agg by passing a dictionary keyed on the columns with the values being the functions to apply to those columns:

df_summary = df.groupby(['Name', 'Site']).agg(
    {'Spend': [np.sum, pd.Series.count], 
     'Spend2': [np.mean, np.std]}
)
df_summary.columns = ['Sum_Spend', 'CountGroupbys_Spend', 'Average_Spend2', 'Standard_Deviation_Spend2']
df_summary = df_summary.reset_index().sort_values(['Site', 'Name'])

>>> df_summary

    Name    Site    Sum_Spend   CountGroupbys_Spend Average_Spend2  Standard_Deviation_Spend2
0   Harry   Amazon  111        3    370.333333      174.081399
4   John    Amazon  43         1    143.000000      NaN
7   Molly   Amazon  97         2    198.500000      78.488853
2   Holly   Apple   69         3    123.000000      11.000000
5   John    Apple   234        1    1234.000000     NaN
1   Harry   Ikea    32         1    632.000000      NaN
3   Holly   Ikea    65         1    365.000000      NaN
6   John    Ikea    97         2    148.500000      7.778175
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • Hey there. Thanks for the answer! I forgot to add one other crucial part. If I just wanted to find the spend and count of two particular columns, and then find the average and standard deviation of another column. How would I do that? I've put an example of my code and Before and After in the original question under "EDIT 10/11/18:" Thanks in advance, Apologies for missing this bit! – semiflex Nov 10 '18 at 00:23