Lambda functions

Lambda functions#

import pandas as pd
pd.set_option('mode.copy_on_write', True)
import matplotlib.pyplot as plt

Here are some estimated global temperature data, from 1850 through 2000.

See the dataset page for more detail.

temperatures = pd.read_csv('data/global_temperature_quarters.csv')
temperatures
Year Temperature
0 1850 13.651
1 1875 13.743
2 1900 13.971
3 1925 13.895
4 1950 13.934
5 1975 14.106
6 2000 14.550

Imagine a function like this:

def degrees_c_to_f(c):
    return c * 1.8 + 32

Notice that degrees_c_to_f is now a value, that is of type function:

degrees_c_to_f
<function __main__.degrees_c_to_f(c)>
degrees_c_to_f(0)
32.0
degrees_c_to_f(10)
50.0
temperatures['Temperature'].apply(degrees_c_to_f)
0    56.5718
1    56.7374
2    57.1478
3    57.0110
4    57.0812
5    57.3908
6    58.1900
Name: Temperature, dtype: float64

Sometimes we don’t want to go to the bother of typing out the whole function with its def and name. In that case we may want to use a lambda function.

A lambda function is a short-hand for defining a function, the doesn’t need a def and does not define a function name. It is a way of giving the recipe in a single expression. For example, here is an expression that defines the same function as we saw above, but using lambda:

lambda c : c * 1.8 + 32
<function __main__.<lambda>(c)>

Notice that this expression, like the def block above, results in an expression. Notice too that this function does not have a name.

We could give this function a name by attaching the result of the expression to a variable, with our usual assignment statement:

c2f_func = lambda c : c * 1.8 + 32

We can then use the function just as we used the def function we defined before:

c2f_func(0)
32.0
c2f_func(10)
50.0

Notice the structure of the lambda expression.

  1. lambda then

  2. the argument(s) to the new function, here c, then

  3. : then

  4. a single expression, here c * 1.8 + 32.

After lambda we have the argument name or names, in our case c. This is the name that the function argument will get when we call the function. For example, when we call c2f_func(0), c will get the value 0.

The colon : signals that the function body follows. For a lambda, the function body must be an expression. Remember, an expression is code that returns a value.

In our case, the expression is c * 1.8 + 32.

temperatures['Temperature'].apply(c2f_func)
0    56.5718
1    56.7374
2    57.1478
3    57.0110
4    57.0812
5    57.3908
6    58.1900
Name: Temperature, dtype: float64

Why would we use this lambda shortcut? Just because it is a shortcut. We could write our whole conversion like this:

temperatures['Temperature'].apply(lambda c : c * 1.8 + 32)
0    56.5718
1    56.7374
2    57.1478
3    57.0110
4    57.0812
5    57.3908
6    58.1900
Name: Temperature, dtype: float64

rather than:

def degrees_c_to_f(c):
    return c * 1.8 + 32

temperatures['Temperature'].apply(degrees_c_to_f)
0    56.5718
1    56.7374
2    57.1478
3    57.0110
4    57.0812
5    57.3908
6    58.1900
Name: Temperature, dtype: float64

More than one argument#

The lambda function above has a single argument, and this is typical of lambda functions that you might use with Pandas apply. But, in general, lambda functions can have more than one argument.

We couldn’t think of a very good and simple example of using two arguments with a lambda, so we hope you’ll forgive us if we use a silly example.

Let’s say you had a function like this:

def times_and_add(x, y):
    return x * y + 10
times_and_add(2, 4)  # 2 * 4 + 10
18
times_and_add(5, -1)  # 5 * -1 + 10
5

times_and_add has two arguments, x and y.

We can write this same function as a lambda with two arguments. Separate the names of the two arguments with a comma before the colon and the function expression:

# name = lambda arg1, arg2 : expression
func = lambda x, y: x * y + 10
func(2, 4)  # 2 * 4 + 10
18
func(5, -1)  # 5 * -1 + 10
5

We can’t as easily use these two-argument lambdas for Pandas apply functions, because apply functions only accept one argument, by default, but it can be done. See the arguments to the apply method for the tools you will need if you do want to do this.