简体   繁体   中英

pandas: how to get the sum of rows by grouping inside a DataFrame?

I am new to Data Science and I am currently using the Pandas library on the Jupyter notebook. Sorry for my poor English.

A,1,5,9
B,2,6,3
A,3,7,2
B,4,8,1

How to group the above CSV values also adding the contents after creating the DataFrame? I want the output something like this.

    A   B
0   4   6
1   12  14
2   11  4

Thanks in advance:)

You can use df.pivot_table

df
   0  1  2  3
0  A  1  5  9
1  B  2  6  3
2  A  3  7  2
3  B  4  8  1

df.pivot_table(columns=0,aggfunc='sum').rename_axis(columns=None)
    A   B
1   4   6
2  12  14
3  11   4

This is groupby + sum with transpose:

print(df.groupby(0).sum().T.rename_axis(columns=None))

    A   B
1   4   6
2  12  14
3  11   4

Note: replace 0 in df.groupby(0) with the actual column name of the first column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM