Examples
Create a series with typical summer temperatures for each city.

In [6]:
import numpy as np
import pandas as pd
In [7]:
s = pd.Series([31, 27, 11],
              index=['Beijing', 'Los Angeles', 'Berlin'])
s
Out[7]:
Beijing        31
Los Angeles    27
Berlin         11
dtype: int64

Square the values by defining a function and passing it as an argument to apply().

In [8]:
def square(x):
    return x ** 2
In [9]:
s.apply(square)
Out[9]:
Beijing        961
Los Angeles    729
Berlin         121
dtype: int64

Square the values by passing an anonymous function as an argument to apply().

In [10]:
s.apply(lambda x: x ** 2)
Out[10]:
Beijing        961
Los Angeles    729
Berlin         121
dtype: int64

Define a custom function that needs additional positional arguments and pass these additional arguments
using the args keyword.

In [11]:
def subtract_custom_value(x, custom_value):
    return x - custom_value
In [12]:
s.apply(subtract_custom_value, args=(4,))
Out[12]:
Beijing        27
Los Angeles    23
Berlin          7
dtype: int64

Define a custom function that takes keyword arguments and pass these arguments to apply.

In [13]:
def add_custom_values(x, **kwargs):
    for month in kwargs:
        x += kwargs[month]
    return x
In [16]:
s.apply(add_custom_values, november=18, december=16, january=15)
Out[16]:
Beijing        80
Los Angeles    76
Berlin         60
dtype: int64

Use a function from the Numpy library.

In [17]:
s.apply(np.log)
Out[17]:
Beijing        3.433987
Los Angeles    3.295837
Berlin         2.397895
dtype: float64