我在下面的代码中创建了我的数据框中的新列,用于销售中位数和已售出的std。我遇到的问题是我不想要多索引输出。如何压缩输出,如下面所需的输出示例?或者有没有办法在不创建多索引的情况下将新列创建为已售出的聚合。
Maybe something like in sql:
select Month, M_day, median(sold) as median, std(sold) as std from month_df group by Month, M_day
Code:
month_df=month_df[['Month','M_day','sold']].groupby(['Month','M_day']).agg(['median','std'])
print(month_df.head())
Output:
sold
median std
Month M_day
1 2 12.0 13.719555
3 5.0 11.508762
4 12.0 12.907761
5 6.0 14.371283
6 12.0 7.859340
desired output:
Month M_day median std
1 2 12.0 13.719555
3 5.0 11.508762
4 12.0 12.907761
5 6.0 14.371283
6 12.0 7.859340