Chaining with Pandas Pipe function
python
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
'x'], 3) np.power(df[
But another way with pipe is:
'x'].pipe(np.power, 3) df[
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.