DEV Community

Salman Mohammed
Salman Mohammed

Posted on Originally published at pythonmysterythings.hashnode.dev

5 Pandas Tricks I Wish I Knew Earlier

If you've spent any time working with data in Python, you already know Pandas is the backbone of most workflows — but a lot of us start by only scratching the surface. When I began, I relied on ⁠ .head() ⁠, basic ⁠ .loc[] ⁠ filtering, and a lot of trial and error.

Here are 5 tricks that would have saved me hours if I'd known them sooner.

1. ⁠ value_counts() ⁠ for Instant Frequency Analysis

Instead of grouping and counting manually, ⁠ value_counts() ⁠ gives you a fast breakdown of category frequencies.

⁠ python

df['category'].value_counts()
Enter fullscreen mode Exit fullscreen mode

 ⁠

Add ⁠ normalize=True ⁠ to get percentages instead of raw counts:

⁠ python

df['category'].value_counts(normalize=True)
Enter fullscreen mode Exit fullscreen mode

 ⁠

This is perfect for quickly understanding class distribution before building a model.

2. ⁠ apply() ⁠ with Lambda for Row-Wise Logic

When a transformation isn't a simple column operation, ⁠ apply() ⁠ with a lambda function lets you write custom logic without a full loop.

⁠ python

df['price_category'] = df['price'].apply(lambda x: 'high' if x > 100 else 'low')
Enter fullscreen mode Exit fullscreen mode

 ⁠

For row-wise logic across multiple columns, use ⁠ axis=1 ⁠:

⁠ python

df['total'] = df.apply(lambda row: row['price'] * row['quantity'], axis=1)
 ⁠
Enter fullscreen mode Exit fullscreen mode

3. ⁠ groupby() ⁠ + ⁠ agg() ⁠ for Multi-Metric Summaries

Rather than running separate ⁠ .mean() ⁠, ⁠ .sum() ⁠, and ⁠ .count() ⁠ calls, combine them in one line:

⁠ python

df.groupby('category').agg({
    'price': 'mean',
    'quantity': 'sum',
    'order_id': 'count'
})
 ⁠
Enter fullscreen mode Exit fullscreen mode

This gives you a clean summary table in a single step — much faster than piecing it together manually.

4. ⁠ pivot_table() ⁠ for Quick Cross-Tabulations

If you've ever wanted an Excel-style pivot table without leaving Python, this is it:

⁠ python

df.pivot_table(values='sales', index='region', columns='month', aggfunc='sum')
 ⁠
Enter fullscreen mode Exit fullscreen mode

Great for spotting trends across two dimensions at once, like sales by region and month.

5. ⁠ pd.cut() ⁠ for Binning Continuous Data

Turning a continuous variable into categories (like age groups or price ranges) is common in EDA and feature engineering:

⁠ python

df['age_group'] = pd.cut(df['age'], bins=[0, 18, 35, 60, 100], labels=['teen', 'young_adult', 'adult', 'senior'])
Enter fullscreen mode Exit fullscreen mode

 ⁠

This beats writing manual if-else conditions and keeps your code readable.

Wrapping Up

None of these tricks are complicated once you know them — that's exactly why they're easy to miss early on. If you're just getting started with Pandas, try working one of these into your next project and see how much cleaner your code becomes.

What Pandas trick took you the longest to discover? Let me know in the comments.

Top comments (0)