How to apply lambda

In this post you will learn how to apply lambda to a dataframe in Pandas.

Applying lambda

I prepared a dataframe in Pandas to which I applied a lambda expression. The lambda expression did a simple increment of the values in my dataframe.

import pandas as pd

my_df = pd.DataFrame({'Column1': [2, 7, 6, 7], 'Column2': [2, 5, 8, 5], 'Column3': [4, 1, 9, 1]})

my_df= my_df.apply(lambda x: x+1)

print(f'This is how to apply lambda in Pandas \n{my_df}')

Now you know how to apply a lambda to a dataframe in Pandas.

See also:
Pandas method documentation

Leave a Reply

Your email address will not be published. Required fields are marked *