---
name: "pandas-multiindex-pivot-tables"
description: "Reshape, pivot, stack, and summarize multidimensional data in Pandas."
metadata:
  author: "Pandas"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "create pivot table pandas"
  - "slice multiindex dataframe"
  - "unstack multiindex pandas"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Pandas MultiIndex Stacking & Pivot Tables

Masters complex data reshaping with Pandas: pd.pivot_table with multiple aggregation functions, unstack/stack levels, cross-tabulations with pd.crosstab, and MultiIndex level slicing with pd.IndexSlice.

## When to Invoke
Invoke this skill whenever you need to:
- create pivot table pandas
- slice multiindex dataframe
- unstack multiindex pandas

## Execution SOP & Rules
1. Construct pivot tables with df.pivot_table(index=['year', 'region'], columns='category', values='sales', aggfunc='sum').
2. Flatten multi-level columns with df.columns = ['_'.join(col).strip() for col in df.columns.values].
3. Slice multi-indexes using pd.IndexSlice rather than chain indexing to avoid SettingWithCopy warnings.
4. Reset indexes explicitly using df.reset_index() when preparing DataFrames for JSON serialization.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
