Sum all rows in a dataframe leaving the first columns
Answers
Answered by
0
Use drop + sum:
df['sum'] = df.drop('gid', axis=1).sum(axis=1)
print (df)
gid col2 col1 col3 sum
0 6 15 45.0 77 137.0
1 1 15 45.0 57 117.0
2 2 14 0.2 42 56.2
3 3 12 6.0 37 55.0
4 4 9 85.0 27 121.0
5 5 5 1.0 15 21.0
If gid is always first column, select by iloc all columns without first and then sum them:
df['sum'] = df.iloc[:, 1:].sum(axis=1)
print (df)
gid col2 col1 col3 sum
0 6 15 45.0 77 137.0
1 1 15 45.0 57 117.0
2 2 14 0.2 42 56.2
3 3 12 6.0 37 55.0
4 4 9 85.0 27 121.0
5 5 5 1.0 15 21.0
✌✌✌✌✌✌
#follow me
Similar questions