Pandas Apply Row Level

This example 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

Creating a simple multiplication function

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 product_x_y
0 16 11 5 176
1 7 10 2 70
2 2 1 12 2
3 16 8 1 128
4 8 17 2 136
5 7 13 3 91