@@ -2204,7 +2204,7 @@ def to_excel(
2204
2204
2205
2205
>>> with pd.ExcelWriter('output.xlsx',
2206
2206
... mode='a') as writer: # doctest: +SKIP
2207
- ... df .to_excel(writer, sheet_name='Sheet_name_3')
2207
+ ... df1 .to_excel(writer, sheet_name='Sheet_name_3')
2208
2208
2209
2209
To set the library that is used to write the Excel file,
2210
2210
you can pass the `engine` keyword (the default engine is
@@ -5864,9 +5864,9 @@ def pipe(
5864
5864
Alternatively a ``(callable, data_keyword)`` tuple where
5865
5865
``data_keyword`` is a string indicating the keyword of
5866
5866
``callable`` that expects the {klass}.
5867
- args : iterable, optional
5867
+ * args : iterable, optional
5868
5868
Positional arguments passed into ``func``.
5869
- kwargs : mapping, optional
5869
+ ** kwargs : mapping, optional
5870
5870
A dictionary of keyword arguments passed into ``func``.
5871
5871
5872
5872
Returns
@@ -5883,25 +5883,70 @@ def pipe(
5883
5883
Notes
5884
5884
-----
5885
5885
Use ``.pipe`` when chaining together functions that expect
5886
- Series, DataFrames or GroupBy objects. Instead of writing
5886
+ Series, DataFrames or GroupBy objects.
5887
5887
5888
- >>> func(g(h(df), arg1=a), arg2=b, arg3=c) # doctest: +SKIP
5888
+ Examples
5889
+ --------
5890
+ Constructing a income DataFrame from a dictionary.
5891
+
5892
+ >>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]]
5893
+ >>> df = pd.DataFrame(data, columns=['Salary', 'Others'])
5894
+ >>> df
5895
+ Salary Others
5896
+ 0 8000 1000.0
5897
+ 1 9500 NaN
5898
+ 2 5000 2000.0
5899
+
5900
+ Functions that perform tax reductions on an income DataFrame.
5901
+
5902
+ >>> def subtract_federal_tax(df):
5903
+ ... return df * 0.9
5904
+ >>> def subtract_state_tax(df, rate):
5905
+ ... return df * (1 - rate)
5906
+ >>> def subtract_national_insurance(df, rate, rate_increase):
5907
+ ... new_rate = rate + rate_increase
5908
+ ... return df * (1 - new_rate)
5909
+
5910
+ Instead of writing
5911
+
5912
+ >>> subtract_national_insurance(
5913
+ ... subtract_state_tax(subtract_federal_tax(df), rate=0.12),
5914
+ ... rate=0.05,
5915
+ ... rate_increase=0.02) # doctest: +SKIP
5889
5916
5890
5917
You can write
5891
5918
5892
- >>> (df.pipe(h)
5893
- ... .pipe(g, arg1=a)
5894
- ... .pipe(func, arg2=b, arg3=c)
5895
- ... ) # doctest: +SKIP
5919
+ >>> (
5920
+ ... df.pipe(subtract_federal_tax)
5921
+ ... .pipe(subtract_state_tax, rate=0.12)
5922
+ ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)
5923
+ ... )
5924
+ Salary Others
5925
+ 0 5892.48 736.56
5926
+ 1 6997.32 NaN
5927
+ 2 3682.80 1473.12
5896
5928
5897
5929
If you have a function that takes the data as (say) the second
5898
5930
argument, pass a tuple indicating which keyword expects the
5899
- data. For example, suppose ``func`` takes its data as ``arg2``:
5900
-
5901
- >>> (df.pipe(h)
5902
- ... .pipe(g, arg1=a)
5903
- ... .pipe((func, 'arg2'), arg1=a, arg3=c)
5904
- ... ) # doctest: +SKIP
5931
+ data. For example, suppose ``national_insurance`` takes its data as ``df``
5932
+ in the second argument:
5933
+
5934
+ >>> def subtract_national_insurance(rate, df, rate_increase):
5935
+ ... new_rate = rate + rate_increase
5936
+ ... return df * (1 - new_rate)
5937
+ >>> (
5938
+ ... df.pipe(subtract_federal_tax)
5939
+ ... .pipe(subtract_state_tax, rate=0.12)
5940
+ ... .pipe(
5941
+ ... (subtract_national_insurance, 'df'),
5942
+ ... rate=0.05,
5943
+ ... rate_increase=0.02
5944
+ ... )
5945
+ ... )
5946
+ Salary Others
5947
+ 0 5892.48 736.56
5948
+ 1 6997.32 NaN
5949
+ 2 3682.80 1473.12
5905
5950
"""
5906
5951
if using_copy_on_write ():
5907
5952
return common .pipe (self .copy (deep = None ), func , * args , ** kwargs )
0 commit comments