Function definition is an important part of software Programming. In python, lambda expressions can be used to anonymously define simple functions. For example, a function that evaluates multiplication can be replaced with a lambda expression. In this post, we will discuss how to define anonymous functions in python using lambda expressions.
Suppose we have a one-line function that returns the product of two input integer values:
def product(x, y):
return x*y
If we call this function with integers 5 and 10 and print the return value we get:
print("Product:",product(5, 10))

Since this is a simple function that does nothing more than evaluate a single expression, "x*y", it can be replaced with a lambda expression:
product_value = lambda x, y: x*y
The lambda expression contains the keyword "lambda", the inputs x and y, a colon, and the expression we want to evaluate. If we look back to our original function definition:
def product(x, y):
return x*y
We see that these expressions look pretty similar. The biggest difference is the absence of the ‘def’ keyword, ‘return’ keyword and function name in our lambda expression.
Let’s print the function with x = 5 and y = 10 as inputs:
print("Product w/ Lambda Expression:",product_value(5,10))

Another interesting application of lambda expressions is in Pandas’ data frames. For this example, we will work with the Movies on Netflix, Prime Video, Hulu and Disney Plus data set, which can be found here.
Let’s read the data into a Pandas data frame:
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
df = pd.read_csv("MoviesOnStreamingPlatforms_updated.csv")
Next, we will print the columns available in this data:
print(df.columns)

Let’s delete the ‘Unamed: 0’ column and print the first five rows of data:
del df['Unnamed: 0']
print(df.head())

We can apply a lambda expression to a single column using the ‘assign()’ method. Suppose we want to create a new column where we remove the ‘%’ symbol from the values in the ‘Rotten Tomatoes’ column. We can do so with a lambda expression. We’ll call our new column ‘rt_ratings’:
df = df.assign(rt_ratings = lambda x: x['Rotten Tomatoes'].str.rstrip('%') )
print(df.head())

Another example is if we wanted to round the ‘IMDb’ rating to the nearest integer. We’ll call our new column ’round_imdb’:
df = df.fillna(0)
df = df.assign(round_imdb = lambda x: x['IMDb'].astype(int))
print(df.head())

I’ll stop here but I encourage you to play around with the data and code yourself.
CONCLUSIONS
To summarize, in this post we discussed how to define anonymous functions using lambda expressions in Python. First we showed how to define an anonymous function that returns the product of two integers. Next we discussed how to apply lambda expressions to Pandas’ data frames to remove characters in column string values and how to round float values to the nearest integer. If you are interested in learning more about data manipulation with pandas, machine learning or even just some of the basics of python programming check out _Python for Data Science and Machine Learning: Python Programming, Pandas and Scikit-learn Tutorials for Beginners._ I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!