# KeyError："['column_name'] not in index"

- **ID:** `python/pandas-keyerror-column`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

引用的列在 DataFrame 中不存在，常因空白、大小写或重命名操作导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   print(df.columns.tolist()) and normalize with df.columns = df.columns.str.strip()
   ```
2. **** (90% 成功率)
   ```
   cols = [c for c in wanted if c in df.columns]; df[cols]
   ```

## 无效尝试

- **** — Still raises KeyError if column truly missing (85% 失败率)
- **** — Silently produces empty results downstream (70% 失败率)
