Pipe

Pandas Basics

1 min read

Published Sep 29 2025, updated Oct 24 2025


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

Output:

   Sales Cost Profit Margin
0 100 50 50 0.500000
1 200 80 120 0.600000
2 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 df

df.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 df

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

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact