大熊猫-更改数据框的格式

Sreejith Menon

我有一个数据帧,其格式为:

level_0 level_1 counts
0   back    not_share   1183
1   back    share   1154
2   back    total   2337
3   front   not_share   697
4   front   share   1073
5   front   total   1770
6   left    not_share   4819
7   left    share   5097
8   left    total   9916
9   other   not_share   2649
10  other   share   2182
11  other   total   4831
12  right   not_share   1449
13  right   share   1744
14  right   total   3193

我想将此表格转换为

level_0 share not_share total
back    1154  1183      2337
front   1073  697       1770 

等等..

是否可以使用任何方法,还是应该将其转换为本地Python数据类型,然后进行操作?

海盗

使用groupbysum

df.groupby(['level_0', 'level_1']).counts.sum().unstack()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章