sum pandas rows code example
Example 1: pandas sum rows
df.sum(axis=1)
df['sums'] = df.iloc[:, 6:23].sum(axis=1)
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])
print(df)
a b c
0 1 2 3
1 4 5 6
2 7 8 9
df['sums'] = df.iloc[:, 1:3].sum(axis=1)
print(df)
a b c sums
0 1 2 3 5
1 4 5 6 11
2 7 8 9 17
Example 2: pandas sum
df.loc['Total'] = pd.Series(df['MyColumn'].sum(), index = ['MyColumn'])
print (df)
X MyColumn Y Z
0 A 84.0 13.0 69.0
1 B 76.0 77.0 127.0
2 C 28.0 69.0 16.0
3 D 28.0 28.0 31.0
4 E 19.0 20.0 85.0
5 F 84.0 193.0 70.0
Total NaN 319.0 NaN NaN