1

I have the following dataframe df:

    A  B  C  D  E

J   4  2  3  2  3
K   5  2  6  2  1
L   2  6  5  4  7

I would like to create an additional column that adds by index the df except column A (which also are numbers), therefore what I have tried is :

df['summation'] = df.iloc[:, 1:4].sum(axis=0)

However, the column summation is added but gives NaN values.

Desired output is:

    A  B  C  D  E  summation

J   4  2  3  2  3    10
K   5  2  6  2  1    11
L   2  6  5  4  7    22

The sum along the row starting at B to the end.

6
  • df.sum(1) you need add on row level. Commented Dec 13, 2017 at 20:29
  • @Wen thnks for your input! but i would like to sum from loc.['B'] 1 along the row Commented Dec 13, 2017 at 20:31
  • Did you try with axis=1? Commented Dec 13, 2017 at 20:32
  • Ok, Your column one is str, so it will not count in sum, you can just do df.sum(1) Commented Dec 13, 2017 at 20:32
  • 2
    The only thing wrong in your approach is the axis. If you pass axis=0 it will take the sum for each column. Since this cannot be aligned with row labels, it will give NaNs. Just change the axis parameter. A label friendly way is df.loc[:, 'B':].sum(axis=1) Commented Dec 13, 2017 at 20:39

1 Answer 1

1

As pointed out in the comments, you apply sum on the wrong axis. If you want to exclude columns from the sum, you can use drop (which also accepts a list of column names which might be handy if you want to exclude columns at e.g. index 0 and 3; then iloc might not be ideal)

df.drop('A', axis=1).sum(axis=1)

which yields

J    10
K    11
L    22

Also @ayhan's solution in the comments works fine.

Sign up to request clarification or add additional context in comments.

Comments

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.