python 3.x - Add values of a column to each other in pandas -


i have df let's 4 cols a,b,c,d(in real 50+ cols). wish add values of cols each other. ex:- df['a'][0]=df['a'][0]+df['a'][1] , on df['b'][0]... .

    col1 col2   col3   1    222    abc b   2    433    dfg c   3    111    tyu d   4    222    iop 

should become :-

    **col1** col2   col3    3     222     abc b    5     433     dfg c    7     111     tyu d    4     222     iop 

i have created loop , after modification assigning result respective cols.

for k,g in colindividual.iteritems():     if(k+1<len(colindividual)):         print(g+colindividual[colindividual.index[k+1]]) 

however, being new python world don't know correct , beautiful way code logic. impact performance in future df might increased more 50 columns. please me here? kind help.

df['col1'].rolling(2).sum().shift(-1).fillna(df['col1']) 

output:

   col1  col2 col3   3.0   222  abc b   5.0   433  dfg c   7.0   111  tyu d   4.0   222  iop 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -