0

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

2
  • Is the datatype of Col1 string? Commented May 29, 2017 at 13:45
  • You can make an iterable over the rows and use the following numpy.sum([float(i) for i in str.split(Col1 , ' ')]) Commented May 29, 2017 at 13:51

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

This is good but,m but this solved my problem col1.sum(axis = 1);;Thanks anyways :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.