Written by: Marlon Colca
Posted on 29 May 2025 - 4 months ago
python pandas analytics
Now it’s time to explore advanced Pandas techniques that will make your code more efficient, expressive, and Pythonic.
Congratulations! You’ve reached the final part of this Pandas series.
So far, we’ve covered everything from loading data to cleaning, grouping, and visualizing it.
Now it’s time to explore advanced Pandas techniques that will make your code more efficient, expressive, and Pythonic.
apply()
for Custom Functionsapply()
lets you run custom functions on columns or rows.
import pandas as pd
df = pd.read_csv("prices_with_missing_data.csv")
# Example: calculate discounted price
df["discounted_price"] = df["price"].apply(lambda x: x * 0.9)
You can also apply a function across rows:
def revenue(row):
return row["price"] * row["quantity_sold"]
df["revenue"] = df.apply(revenue, axis=1)
map()
for Element-wise Operationsmap()
is simpler than apply()
when working with a single Series.
df["brand_upper"] = df["brand"].map(str.upper)
You can also map dictionaries for replacements:
brand_map = {"Brand A": "Premium A", "Brand B": "Budget B"}
df["brand"] = df["brand"].map(brand_map).fillna(df["brand"])
query()
for Cleaner FilteringInstead of complex boolean indexing, you can use SQL-like queries.
# Products in stock and cheaper than 5
cheap_stock = df.query("in_stock == True and price < 5")
This makes your filters more readable.
Avoid using Python for
loops with Pandas. Vectorized operations are much faster.
# Bad (slow)
df["revenue_loop"] = [p*q for p,q in zip(df["price"], df["quantity_sold"])]
# Good (fast, vectorized)
df["revenue_vec"] = df["price"] * df["quantity_sold"]
Instead of creating temporary variables, you can chain methods:
summary = (
df.dropna()
.query("price > 2")
.groupby("brand")["revenue"]
.sum()
.sort_values(ascending=False)
)
This style (called method chaining) is concise and easier to read.
.astype("category")
for categorical columns → saves memory..copy()
when creating new DataFrames to avoid warnings..merge()
and .join()
to combine datasets efficiently.In this final part, you learned how to:
apply()
and map()
for flexible transformationsquery()
🎉 Congratulations — you’ve completed the 10-part Pandas series!
You now have a solid foundation to work confidently with data in Python.
Keep practicing, explore more datasets, and share your insights with others.
Data is powerful when you know how to use it. 🚀