简体   繁体   中英

sum for every row values through columns pandas

This is my dataframe and I want to sum for every row values through columns A,B,C,D and append column 'Summ'

 ABCD Summ 0 1 1 0 0 1+1+0+0 1 0 0 1 1 0+0+1+1 2 0 0 1 0 0+0+1+0 3 1 1 1 1 1+1+1+1 4 1 0 1 0 1+0+1+0 

df['Summ'] = df.sum(axis=1)

or better:

df.loc[:, 'Summ'] = df.sum(axis=1)

or for a subset of columns

cols = ['A','B']
df.loc[:, 'Summ'] = df[cols].sum(axis=1)

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