命令如下:df=pd.read_excel('top8.xlsx')df显示如下abcdefgcustomproduct0429.6019.8732.217.1293127.7520429.6111429.6019.8732.217.1293127.7520429.6122214.8019.8716.117.129311...
命令如下:
df = pd.read_excel('top8.xlsx')
df
显示如下
a b c d e f g custom product
0 429.60 19.87 32.2 17.12931 27.75 20 429.6 1 1
1 429.60 19.87 32.2 17.12931 27.75 20 429.6 1 2
2 214.80 19.87 16.1 17.12931 13.88 10 214.8 1 1
3 1,074.00 19.87 80.5 17.12931 69.39 50 1074.0 1 2
4 214.80 19.87 16.1 17.12931 13.88 10 214.8 2 1
5 429.60 19.87 32.2 17.12931 27.75 20 429.6 2 2
6 1,074.00 19.87 80.5 17.12931 69.39 50 1074.0 2 1
7 429.60 19.87 32.2 17.12931 27.75 20 429.6 2 2
要对custom和product作为键对a-g列求和
命令如下:
df.groupby(['custom','product']).sum()
显示如下:
custom product b c d e f g
1 1 39.74 48.3 34.25862 41.63 30 644.4
2 39.74 112.7 34.25862 97.14 70 1503.6
2 1 39.74 96.6 34.25862 83.27 60 1288.8
2 39.74 64.4 34.25862 55.50 40 859.2
结果中没有对a列求和,怎么解决?
我尝试单独对a列求和
df.groupby(['custom','product'])[['a']].sum()
输出如下:
custom product a
1 1 429.60214.80
2 429.601,074.00
2 1 214.801,074.00
2 429.60429.60
pandas把a列结果直接连接起来了,怎么解决?
展开