Reputation: 2273
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.
Upvotes: 1
Views: 66
Reputation: 26069
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.
Upvotes: 1