Examples
import numpy as np
import pandas as pd
df = pd.DataFrame([[9, 25]] * 3, columns=['P', 'Q'])
df
Using a numpy universal function (in this case the same as np.sqrt(df)):
df.apply(np.sqrt)
Using a reducing function on either axis
df.apply(np.sum, axis=0)
df.apply(np.sum, axis=1)
Returning a list-like will result in a Series:
df.apply(lambda x: [1, 2], axis=1)
Passing result_type=’expand’ will expand list-like results to columns of a Dataframe:
df.apply(lambda x: [1, 2], axis=1, result_type='expand')
Returning a Series inside the function is similar to passing result_type='expand'.
The resulting column names will be the Series index.
df.apply(lambda x: pd.Series([1, 2], index=['f1', 'b1']), axis=1)
Passing result_type='broadcast' will ensure the same shape result, whether list-like
or scalar is returned by the function, and broadcast it along the axis. The resulting
column names will be the originals.
df.apply(lambda x: [1, 2], axis=1, result_type='broadcast')