1. Introduction: Column and Table Transformations
Pandas offers three main tools for transforming features:
map() — fast transforms on a single Series
apply() — custom logic, often with if/else or multi-column needs
pipe() — chain whole-DataFrame steps into a readable pipeline
2. Dataset Overview
Example context: an e-commerce table with identifiers, categories, quantity, price, payment method, delivery days, rating, and revenue—mix of numbers, dates, and text.
3. map(): Fast Single-Column Transforms
map() works on one Series. Ideal for dictionary lookups and category recoding.
import pandas as pd
df = pd.read_csv("ecommerce_orders.csv")
payment_mapping = {
"Card": "Digital",
"Wallet": "Digital",
"Cash on Delivery": "Cash"
}
df["Payment Type"] = df["Payment Method"].map(payment_mapping)
print(df[["Payment Method", "Payment Type"]].head())
Card and Wallet become Digital; Cash on Delivery becomes Cash.
4. apply(): Custom Logic
apply() can take a function with branching. Example: bin revenue into High / Medium / Low:
def categorize_revenue(val):
if val > 500:
return "High"
elif val >= 200:
return "Medium"
else:
return "Low"
df["Revenue Category"] = df["Revenue"].apply(categorize_revenue)
print(df["Revenue Category"].value_counts())
Illustrative counts on a 5,000-row sample: High ~3,252, Medium ~1,298, Low ~650.
5. pipe(): Sequential Pipelines
Chain DataFrame-level functions in order:
def add_profit(data):
data["Profit"] = data["Revenue"] * 0.20
return data
def add_net_revenue(data):
data["Net Revenue"] = data["Revenue"] - data["Profit"]
return data
df_processed = (
df
.pipe(add_profit)
.pipe(add_net_revenue)
)
print(df_processed[["Revenue", "Profit", "Net Revenue"]].head())
First step adds Profit (20% of revenue); second adds Net Revenue (Revenue − Profit).
6. map vs apply vs pipe
| Function | Level | Best for |
| map() | Single Series | Fast dict/function mapping of categories |
| apply() | Series or DataFrame | Custom functions, if/else, multi-column logic |
| pipe() | Whole DataFrame | Ordered multi-step ETL pipelines |