# Pandas Cheat Sheet
_The operations that cover most day-to-day dataframe work_
A working reference for selection, cleaning, grouping and reshaping — the four things almost every pandas session is made of.
> Difficulty: beginner  
> Version: 1.0  
> Updated: 2025-09-17  
> Categories: Python  
> Tags: Cli

Source: https://invitationbuddy.com/cheat-sheet/pandas-cheat-sheet

---

## Selecting
| Expression | Returns |
| --- | --- |
| `df["col"]` | One column as a Series |
| `df[["a", "b"]]` | Several columns as a DataFrame |
| `df.loc[rows, cols]` | By LABEL — the end of a slice is included |
| `df.iloc[0:5, 0:3]` | By POSITION — the end is excluded |
| `df[df["age"] > 30]` | Boolean mask |
| `df.query("age > 30 and city == 'Oslo'")` | The same, readably |
| `df.loc[df["a"] > 1, "b"] = 0` | The assignment form that is always safe |

## Cleaning and grouping
| Expression | Does |
| --- | --- |
| `df.isna().sum()` | Count missing values per column |
| `df.dropna(subset=["a"])` | Drop rows missing a specific column |
| `df.fillna({"a": 0})` | Fill per column, not globally |
| `df.drop_duplicates(subset=["id"], keep="last")` | De-duplicate on a key |
| `df.groupby("k").agg(n=("x", "size"), avg=("x", "mean"))` | Named aggregations — readable output columns |
| `df.merge(other, on="id", how="left", indicator=True)` | Join; _merge shows what matched |
| `df.pivot_table(index="a", columns="b", values="v", aggfunc="sum")` | Reshape wide |

## SettingWithCopyWarning
The warning means pandas cannot tell whether you are writing to the original frame or to a temporary copy — so your edit may silently vanish. It is caused by chained indexing: df[df.a > 1]["b"] = 0. Write it as one .loc call instead: df.loc[df.a > 1, "b"] = 0. If you genuinely wanted a separate frame, make that explicit with .copy().

## Code examples
### Group, aggregate and rank in one pass
Named aggregations give readable output columns; rank() then orders inside each group.
```python
import pandas as pd

summary = (
    df.groupby('category')
      .agg(
          n=('price', 'size'),
          avg_price=('price', 'mean'),
          top_price=('price', 'max'),
      )
      .reset_index()
)

# Rank WITHIN each category, best first
df['rank'] = (
    df.groupby('category')['price']
      .rank(method='dense', ascending=False)
      .astype(int)
)

# Safe conditional assignment — one .loc, never chained indexing
df.loc[df['price'] > 100, 'tier'] = 'premium'
```

## FAQs
**What causes SettingWithCopyWarning?**
Chained indexing — df[mask]["col"] = value — where pandas cannot tell whether you are writing to the original frame or a temporary copy. Write it as one .loc call instead.

**Should I use loc or iloc?**
loc for labels, iloc for positions. The trap is that loc INCLUDES the end of a slice while iloc excludes it, exactly like the rest of Python.

---
_Generated from https://invitationbuddy.com/cheat-sheet/pandas-cheat-sheet_
