0

I have a following dataframe:

     d = {'City' : ['Paris', 'London', 'NYC', 'Paris', 'NYC'], 'ppl' : [3000,4646,33543,85687568,34545]}
    df = pd.DataFrame(data=d)
    df_mean = df.groupby('City').mean()

now I want to instead just calc the mean (and .std()) of the ppl column, I want to have the city, mean, std in my dataframe (of course the cities should be grouped). If this is not possible it would be ok to just add at least the column for the .std() column to my resulting dataframe

Florent
  • 355
  • 2
  • 12

1 Answers1

1

You can use .GroupBy.agg(), as follows:

df.groupby('City').agg({'ppl': ['min', 'std']})

If you don't want the column City be the index, you can do:

df.groupby('City').agg({'ppl': ['min', 'std']}).reset_index()

or

df.groupby('City')['ppl'].agg(['mean','std']).reset_index()

Result:

     City      mean           std
0  London      4646           NaN
1     NYC     34044  7.085210e+02
2   Paris  42845284  6.058814e+07
SeaBean
  • 22,547
  • 3
  • 13
  • 25