Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas : Sum multiple columns and get results in multiple columns

I have a "sample.txt" like this.

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

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 )
like image 466
Akio Omi Avatar asked Dec 04 '25 18:12

Akio Omi


1 Answers

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()
like image 99
piRSquared Avatar answered Dec 06 '25 07:12

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!