pandas.DataFrame.pipe#
- DataFrame.pipe(func, *args, **kwargs)[source]#
Apply chainable functions that expect Series or DataFrames.
- Parameters
- funcfunction
Function to apply to the Series/DataFrame.
args, andkwargsare passed intofunc. Alternatively a(callable, data_keyword)tuple wheredata_keywordis a string indicating the keyword ofcallablethat expects the Series/DataFrame.- argsiterable, optional
Positional arguments passed into
func.- kwargsmapping, optional
A dictionary of keyword arguments passed into
func.
- Returns
- the return type of
func.
- the return type of
See also
DataFrame.applyApply a function along input axis of DataFrame.
DataFrame.applymapApply a function elementwise on a whole DataFrame.
Series.mapApply a mapping correspondence on a
Series.
Notes
Use
.pipewhen chaining together functions that expect Series, DataFrames or GroupBy objects. Instead of writing>>> func(g(h(df), arg1=a), arg2=b, arg3=c)
You can write
>>> (df.pipe(h) ... .pipe(g, arg1=a) ... .pipe(func, arg2=b, arg3=c) ... )
If you have a function that takes the data as (say) the second argument, pass a tuple indicating which keyword expects the data. For example, suppose
functakes its data asarg2:>>> (df.pipe(h) ... .pipe(g, arg1=a) ... .pipe((func, 'arg2'), arg1=a, arg3=c) ... )