如何总结此数据框。
OCT NOV DEC JAN Feb Mar Apr May Jun Jul Aug Sep Total Sales
0 0 0 0 0 1 0 0 0 0 0 0 0 4825379.818
1 0 0 0 0 1 0 0 0 0 0 0 0 5522382.297
2 0 0 0 0 1 0 0 0 0 0 0 0 5721183.289
3 0 0 0 0 1 0 0 0 0 0 0 0 5374047.083
4 0 0 0 0 0 1 0 0 0 0 0 0 4917456.971
我希望它的格式为
Month Sales
JAN 1235
FEB 5454
.. ..
..
答案 0 :(得分:0)
您可以先将mul
提取的列乘以pop
,再对sum
行Series
行,对大写的月份名称行rename
,再添加{{3 }}表示正确的顺序,reindex
表示带有rename_axis
的nw列名称:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00