pandas :总结多个列,并在多列中获得结果 [英] Pandas : Sum multiple columns and get results in multiple columns

查看:141
本文介绍了 pandas :总结多个列,并在多列中获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a "sample.txt" like this.

我有这样的sample.txt x
K 4 5 6 2 x
L 7 8 9 3 y
M 1 2 3 4 y
N 4 5 6 5 z
O 7 8 9 6 z

idx A B C D cat J 1 2 3 1 x K 4 5 6 2 x L 7 8 9 3 y M 1 2 3 4 y N 4 5 6 5 z O 7 8 9 6 z

使用这个数据集,我想得到行和列的总和。
在行中,这不是什么大问题。
我做了这样的结果。

With this dataset, I want to get sum in row and column. In row, it is not a big deal. I made result like this.

### MY CODE ###
import pandas as pd

df = pd.read_csv('sample.txt',sep="\t",index_col='idx')
df.info()

df2 = df.groupby('cat').sum()
print( df2 )



<

The result is like this.

      A   B   C   D
cat                
x     5   7   9   3
y     8  10  12   7
z    11  13  15  11

但我不知道如何写一个代码来获得这样的结果。
(只需在列A和B以及C列和D列中添加值)

But I don't know how to write a code to get result like this. (simply add values in column A and B as well as column C and D)

    AB  CD
J   3   4
K   9   8
L   15  12
M   3   7
N   9   11
O   15  15

有人可以帮忙写一段代码吗?

Could anybody help how to write a code?

顺便说一句,我没有想要这样做。
(它看起来太无聊了,但如果它是唯一的方式,我会认为它)

By the way, I don't want to do like this. (it looks too dull, but if it is the only way, I'll deem it)

df2 = df['A'] + df['B']
df3 = df['C'] + df['D']
df = pd.DataFrame([df2,df3],index=['AB','CD']).transpose()
print( df )


推荐答案

当您将字典或可调用字符传递给 groupby 时,它将应用于轴。我指定了一个是列的轴。

When you pass a dictionary or callable to groupby it gets applied to an axis. I specified axis one which is columns.

d = dict(A='AB', B='AB', C='CD', D='CD')
df.groupby(d, axis=1).sum()

这篇关于 pandas :总结多个列,并在多列中获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆