加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – pandas – 更改数据框的格式

发布时间:2020-12-20 11:54:17 所属栏目:Python 来源:网络整理
导读:我有一个数据框格式: level_0 level_1 counts0 back not_share 11831 back share 11542 back total 23373 front not_share 6974 front share 10735 front total 17706 left not_share 48197 left share 50978 left total 99169 other not_share 264910 othe
我有一个数据框格式:

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数据类型然后进行操作?

解决方法

使用groupby和sum

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

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读