pandas add row sum of columns 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: add rows to dataframe pandas
df = df.append({'a':1, 'b':2}, ignore_index=True)
Example 3: pandas row sum
df.sum(axis=1)
Example 4: python how to add columns to a pandas dataframe
pandas_dataframe['new_column_name'] = ['list', 'of', 'column', 'values']
pandas_dataframe['new_column_name'] = value