I have a df =
statistics s_values
year
1999 cigarette use 100
1999 cellphone use 310
1999 internet use 101
1999 alcohol use 100
1999 soda use 215
2000 cigarette use 315
2000 cellphone use 317
2000 internet use 325
2000 alcohol use 108
2000 soda use 200
2001 cigarette use 122
2001 cellphone use 311
2001 internet use 112
2001 alcohol use 144
2001 soda use 689
I calculated the max, min and mean based on the year index and statistics column.
I want to insert the mean, max and min as columns in the data frame where the output result looks like this
my desired output:
statistics s_values mean min max
year
1999 alcohol use 100.0 104.0 100.0 108.0
1999 cellphone use 310.0 313.5 310.0 317.0
1999 cigarette use 100.0 207.5 100.0 315.0
1999 internet use 101.0 213.0 101.0 325.0
1999 soda use 215.0 207.5 200.0 215.0
2000 alcohol use 108.0 104.0 100.0 108.0
2000 cellphone use 317.0 313.5 310.0 317.0
2000 cigarette use 315.0 207.5 100.0 315.0
2000 internet use 325.0 213.0 101.0 325.0
2000 soda use 200.0 207.5 200.0 215.0
2001 alcohol use 144.0 104.0 100.0 108.0
2001 cellphone use 311.0 313.5 310.0 317.0
2001 cigarette use 122.0 207.5 100.0 315.0
2001 internet use 112.0 213.0 101.0 325.0
2001 soda use 689.0 207.5 200.0 215.0
I tried to do the following but values in the columns are all NaN
gen_mean = df.groupby('statistics').mean()
gen_min = df.groupby('statistics').min()
gen_max = df.groupby('statistics').max()
df.insert(2, 'Gen Avg', gen_mean)
df.insert(3, 'Gen Max', gen_max)
df.insert(4, 'Gen Min', gen_min)
Thank you