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