bigframes.pandas.DataFrame.pipe#
- DataFrame.pipe(func: Callable[..., T] | tuple[Callable[..., T], str], *args, **kwargs) T#
Apply chainable functions that expect Series or DataFrames.
Examples:
Constructing a income DataFrame from a dictionary.
>>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]] >>> df = bpd.DataFrame(data, columns=['Salary', 'Others']) >>> df Salary Others 0 8000 1000.0 1 9500 <NA> 2 5000 2000.0 [3 rows x 2 columns]
Functions that perform tax reductions on an income DataFrame.
>>> def subtract_federal_tax(df): ... return df * 0.9 >>> def subtract_state_tax(df, rate): ... return df * (1 - rate) >>> def subtract_national_insurance(df, rate, rate_increase): ... new_rate = rate + rate_increase ... return df * (1 - new_rate)
Instead of writing
>>> subtract_national_insurance( ... subtract_state_tax(subtract_federal_tax(df), rate=0.12), ... rate=0.05, ... rate_increase=0.02)
You can write
>>> ( ... df.pipe(subtract_federal_tax) ... .pipe(subtract_state_tax, rate=0.12) ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02) ... ) Salary Others 0 5892.48 736.56 1 6997.32 <NA> 2 3682.8 1473.12 [3 rows x 2 columns]
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
national_insurancetakes its data asdfin the second argument:>>> def subtract_national_insurance(rate, df, rate_increase): ... new_rate = rate + rate_increase ... return df * (1 - new_rate) >>> ( ... df.pipe(subtract_federal_tax) ... .pipe(subtract_state_tax, rate=0.12) ... .pipe( ... (subtract_national_insurance, 'df'), ... rate=0.05, ... rate_increase=0.02 ... ) ... ) Salary Others 0 5892.48 736.56 1 6997.32 <NA> 2 3682.8 1473.12 [3 rows x 2 columns]
- Parameters:
func (function) – Function to apply to this object.
args, andkwargsare passed intofunc. Alternatively a(callable, data_keyword)tuple wheredata_keywordis a string indicating the keyword ofcallablethat expects this object.args (iterable, optional) – Positional arguments passed into
func.kwargs (mapping, optional) – A dictionary of keyword arguments passed into
func.
- Returns:
Object of same type as caller
- Return type: