pythonintermediate
Pandas Method Chaining with .pipe()
Use the .pipe() method to create clean, readable pandas transformation chains.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
def add_revenue(df):
df['revenue'] = df['price'] * df['qty']
return df
def drop_nulls(df, cols):
return df.dropna(subset=cols)
def normalise_names(df):
df.columns = df.columns.str.lower().str.replace(' ', '_')
return df
result = (
pd.read_csv('sales.csv')
.pipe(normalise_names)
.pipe(drop_nulls, cols=['price', 'qty'])
.pipe(add_revenue)
.query('revenue > 100')
.sort_values('revenue', ascending=False)
.reset_index(drop=True)
)
print(result.head())Use Cases
- clean ETL code
- reproducible transformations
- data preprocessing
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Read Large CSV in Chunks with Pandas
Process CSV files larger than RAM by reading in chunks — memory-efficient ETL pattern for data pipelines.
Best for: Processing multi-GB CSV files without running out of memory
#pandas#csv
pythonintermediate
Pandas Vectorised Operations vs Apply
Compare apply vs vectorised pandas operations for performance-critical column transformations.
Best for: feature engineering
#pandas#vectorization
pythonbeginner
SQLite + Pandas Local Data Pipeline
Run a lightweight local ETL with SQLite and pandas: load CSV, transform, persist to SQLite.
Best for: local analytics
#sqlite#pandas
pythonbeginner
Flatten Nested JSON with pandas
Use pd.json_normalize to flatten deeply nested API responses into a flat DataFrame.
Best for: API response flattening
#pandas#json