Pipe

Pandas Basics

1 min read

Published Sep 29 2025, updated Aug 17 2026


21
0
0
0

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)))

You can write:

df.pipe(func1).pipe(func2).pipe(func3)

More readable, especially with many transformations.






Syntax

DataFrame.pipe(func, *args, **kwargs)

  • func: function to apply. Must take the DataFrame (or Series) as its first argument.
  • *args, **kwargs: additional arguments passed to func.





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 pddf = pd.DataFrame({    "Sales": [100, 200, 300],    "Cost": [50, 80, 120]})def add_profit(df):    df["Profit"] = df["Sales"] - df["Cost"]    return dfdef add_margin(df):    df["Margin"] = df["Profit"] / df["Sales"]    return dfdf_new = (    df    .pipe(add_profit)    .pipe(add_margin))print(df_new)

Output:

   Sales  Cost  Profit    Margin0    100    50      50  0.5000001    200    80     120  0.6000002    300   120     180  0.600000





Using Arguments with .pipe()

You can pass arguments to your function after the DataFrame:

def add_column(df, colname, value):    df[colname] = value    return dfdf.pipe(add_column, "Discount", 0.1)





Combining with lambda

For inline transformations:

df.pipe(lambda d: d.assign(Revenue=d["Sales"] * 1.2))





Debugging with .pipe(print)

(df .pipe(add_profit) .pipe(print)  .pipe(add_margin))

Prints intermediate DataFrame without breaking the chain.






Example use cases

Data Cleaning Pipelines:

def clean_names(df):    df.columns = df.columns.str.lower()    return dfdef drop_nulls(df):    return df.dropna()df_clean = df.pipe(clean_names).pipe(drop_nulls)


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")


Outlier Removal:

def drop_outliers(df, col, threshold=3):    return df[df[col] < threshold]df = (    df    .pipe(drop_outliers, col="Cost", threshold=100))
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Pandas Basics | Pipe | SimpleSteps.guide