Pipe
Pandas Basics
1 min read
This section is 1 min read, full guide is 30 min read
Published Sep 29 2025, updated Oct 24 2025
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
PandasPython
What is .pipe()?
.pipe()lets you apply a function (or sequence of functions) to a DataFrame (or Series) inside a method chain.- Instead of breaking up transformations step by step, you can write them in a clean, functional style.
- It makes code more readable and reusable.
- Works on whole DataFrame, as apposed to
.apply()that works on just rows or columns.
Instead of writing:
df = func3(func2(func1(df)))
Copy to Clipboard
You can write:
df.pipe(func1).pipe(func2).pipe(func3)
Copy to Clipboard
More readable, especially with many transformations.
Syntax
DataFrame.pipe(func, *args, **kwargs)
Copy to Clipboard
func: function to apply. Must take the DataFrame (or Series) as its first argument.*args,**kwargs: additional arguments passed tofunc.
Why Use .pipe()?
- Improves readability in long chains.
- Encourages a functional style (functions return new DataFrames instead of mutating).
- Makes it easier to reuse and test transformations.
- Useful when combining Pandas with custom helper functions or method chaining (e.g., cleaning, feature engineering).
Basic Example
import pandas as pd
df = pd.DataFrame({
"Sales": [100, 200, 300],
"Cost": [50, 80, 120]
})
def add_profit(df):
df["Profit"] = df["Sales"] - df["Cost"]
return df
def add_margin(df):
df["Margin"] = df["Profit"] / df["Sales"]
return df
df_new = (
df
.pipe(add_profit)
.pipe(add_margin)
)
print(df_new)
Copy to Clipboard
Output:
Sales Cost Profit Margin
0 100 50 50 0.500000
1 200 80 120 0.600000
2 300 120 180 0.600000
Copy to Clipboard
Using Arguments with .pipe()
You can pass arguments to your function after the DataFrame:
def add_column(df, colname, value):
df[colname] = value
return df
df.pipe(add_column, "Discount", 0.1)
Copy to Clipboard
Combining with lambda
For inline transformations:
df.pipe(lambda d: d.assign(Revenue=d["Sales"] * 1.2))
Copy to Clipboard
Debugging with .pipe(print)
(df
.pipe(add_profit)
.pipe(print)
.pipe(add_margin)
)
Copy to Clipboard
Prints intermediate DataFrame without breaking the chain.
Example use cases
Data Cleaning Pipelines:
def clean_names(df):
df.columns = df.columns.str.lower()
return df
def drop_nulls(df):
return df.dropna()
df_clean = df.pipe(clean_names).pipe(drop_nulls)
Copy to Clipboard
Reusable Reporting;
def filter_by_date(df, start, end):
return df.loc[start:end]
df.pipe(filter_by_date, "2025-01-01", "2025-01-31")
Copy to Clipboard
Outlier Removal:
def drop_outliers(df, col, threshold=3):
return df[df[col] < threshold]
df = (
df
.pipe(drop_outliers, col="Cost", threshold=100)
)
Copy to Clipboard














