Chaining with Pandas Pipe function

python
Published

November 30, 2020

I often use method chaining in pandas, although certain problems like calculating the second most common value are hard. A really good solution to adding custom functionality in a chain is Pandas pipe function.

For example to raise a function to the 3rd power with numpy you could use

np.power(df['x'], 3)

But another way with pipe is:

df['x'].pipe(np.power, 3)

Note that you can pass any positional or keyword arguments and they’ll get passed along. So df.pipe(f, *args, **kwargs) is equivalent to f(df, *args, **kwargs).

If you build up a series of transforms on dataframes to dataframes you can then express this with a series of pipe statements. It even works with a groupby.