Pandas Apply Row Level

This examples demonstrates using the apply function for operating on rows. First we create the dataset.

import numpy as np
import pandas as pd
sample_df = pd.DataFrame( data=np.random.randint(1, 20, 18).reshape(6, 3),
                          columns=['x-val', 'y-val', 'z-val'])
sample_df
x-val y-val z-val
0 16 11 5
1 7 10 2
2 2 1 12
3 16 8 1
4 8 17 2
5 7 13 3
def AreaFunc(x):
    return x['x-val'] * x['y-val']
                
sample_df['product_x_y'] = sample_df.apply(AreaFunc, axis=1)
sample_df
x-val y-val z-val new_col product_x_y
0 16 11 5 high 176
1 7 10 2 low 70
2 2 1 12 low 2
3 16 8 1 high 128
4 8 17 2 low 136
5 7 13 3 low 91