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.
df.sum(1)you need add on row level.loc.['B']1 along the rowaxis=1?axis=0it 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 isdf.loc[:, 'B':].sum(axis=1)