C.W.K.
Stream
Lesson 07 of 07 · published

Method Chaining and assign() — Readable Transforms

~11 min · pandas, style, method-chaining

Level 0Curious Reader
0 XP0/47 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Pipelines that read like prose

Pandas code written one statement at a time tends to grow into a thicket of intermediate variables — df1, df2, df_filtered, df_filtered_grouped. The modern style is method chaining: every step returns a new DataFrame, you indent, and the pipeline reads top-to-bottom like a sequence of operations.

The two enabling methods are assign() (add or overwrite columns without mutating the input) and pipe() (apply an arbitrary function in the chain). Combined with query(), groupby(), and sort_values(), you can express most analytical pipelines as one indented expression.

Code

From spaghetti to chain — same logic, very different readability·python
import pandas as pd

# --- Spaghetti style (resist this) ---
raw = pd.read_csv('orders.csv')
raw['order_date'] = pd.to_datetime(raw['order_date'])
filtered = raw[raw['status'] == 'completed']
filtered = filtered[filtered['amount_usd'] > 0]
filtered['month'] = filtered['order_date'].dt.to_period('M')
monthly = filtered.groupby('month', as_index=False).agg(
    revenue=('amount_usd', 'sum'),
    orders=('order_id', 'nunique'),
)
monthly = monthly.sort_values('month')

# --- Chained style (do this) ---
monthly = (
    pd.read_csv('orders.csv')
      .assign(order_date=lambda d: pd.to_datetime(d['order_date']))
      .query("status == 'completed' and amount_usd > 0")
      .assign(month=lambda d: d['order_date'].dt.to_period('M'))
      .groupby('month', as_index=False)
      .agg(revenue=('amount_usd', 'sum'),
           orders=('order_id', 'nunique'))
      .sort_values('month')
)
Use <code>pipe()</code> to plug your own functions into the chain·python
def attach_country(df: pd.DataFrame, customers: pd.DataFrame) -> pd.DataFrame:
    return df.merge(customers[['customer_id', 'country']],
                    on='customer_id', how='left', validate='many_to_one')

result = (
    orders
      .pipe(attach_country, customers=customers)
      .query("country == 'KR'")
      .groupby('order_date', as_index=False)['amount_usd'].sum()
)

External links

Exercise

Take any non-trivial Pandas script of yours and rewrite it as a single chained expression. Name only the inputs and the final output. If you can't (e.g. because two branches share a midpoint), that midpoint is the one variable that earns a name. Notice how the chain forces you to design the pipeline before you write it.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.