Pandas Apply Using Lambda Expression

In this example, we demonstrate the use of apply function using the lambda function.

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 9 7 2
1 19 4 18
2 7 13 13
3 10 6 2
4 15 3 17
5 3 10 7

Implementing the apply function with lambda expression

sample_df['new'] = sample_df['x-val'].apply(lambda x: x/100)
sample_df
x-val y-val z-val new
0 9 7 2 0.09
1 19 4 18 0.19
2 7 13 13 0.07
3 10 6 2 0.10
4 15 3 17 0.15
5 3 10 7 0.03