Andre_k
Andre_k

Reputation: 1730

Adding multiple elements of a single columns of dataframe in python

dataframe = ax

Col1
0.98 0.076 0.09
0.81 0.01  0.378
0.12 0.33 0.001

I want to add these multiple elements of a single row which are separated by space in python such that my output be like

   Col1                    Summm
0.98 0.076 0.09        0.98+0.076+0.09          
0.81 0.01  0.378       0.81+0.01+0.378
0.12 0.33 0.001        0.12+0.33+0.001

I have tried this

summ = numpy.sum(array[0:len(ax),1:len(ax[0])],axis=1).tolist()

but this doesnt giv me proper output Any help would be much appreciated..Thanks

Upvotes: 0

Views: 254

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Let's try:

df.assign(Summm=df.Col1.str.split('\s+',expand=True).astype(float).sum(1))

Output:

               Col1  Summm
0   0.98 0.076 0.09  1.146
1  0.81 0.01  0.378  1.198
2   0.12 0.33 0.001  0.451

Upvotes: 1

Related Questions