Pandas and scikit-learn KeyError .... not in index
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Pandas error KeyError: ... not in index usually means your code is trying to select columns or rows that do not exist in the current DataFrame. In machine-learning workflows with scikit-learn, this often happens after feature selection, one-hot encoding, train-test splitting, or column reordering changes the available labels.
Understand what the error is really saying
Pandas indexes rows and columns by labels. If you ask for labels that are missing, Pandas raises KeyError.
A simple example:
This fails because height is not a column in the frame.
The first debugging step is always to inspect the current labels:
Do that at the point of failure, not only earlier in the pipeline.
Common scikit-learn workflow cause: train and test columns drift
One of the most frequent machine-learning causes is fitting transformations on one frame and trying to index another frame with columns that no longer match.
For example:
This can fail if the test set does not contain every dummy column that appeared in training.
A safer fix is to align the frames:
Now both frames share the same columns, with missing columns filled by zeros.
Check for dropped or renamed columns
Another common pattern is:
- select features
- drop a column
- later try to access the old feature list again
That raises KeyError because city was already removed.
The fix is either to update the feature list or to derive it from the current frame:
That pattern is defensive, though in many production code paths it is even better to fail early with a clear custom message.
Reindex when you want missing columns filled
If your intent is "select these columns if they exist, and create missing ones as zeros," use reindex instead of direct indexing.
This is especially useful after one-hot encoding or feature engineering pipelines.
Prefer scikit-learn pipelines for stable feature handling
A robust long-term fix is to let scikit-learn manage preprocessing consistently through a pipeline and ColumnTransformer.
With handle_unknown="ignore", unseen categories in test data do not create the same column-mismatch pain that manual get_dummies workflows often do.
Index mistakes are not only about columns
The error can also come from row-label selection:
If row labels 10 and 11 do not exist, Pandas raises KeyError.
Make sure you know whether you mean:
- label-based access with
.loc - position-based access with
.iloc
That distinction matters a lot after filtering and splitting data.
Common Pitfalls
The biggest mistake is inspecting the original DataFrame instead of the transformed one that actually raised the error. In data pipelines, the label set changes frequently.
Another issue is using pd.get_dummies separately on train and test data without aligning the resulting columns. That is a classic cause of missing dummy features.
Developers also confuse .loc with .iloc. If you want positional selection, label-based indexing will fail as soon as the index labels differ from the integer positions you expect.
Finally, a feature list copied into code can drift away from reality over time. If the pipeline changes, make sure the selection logic changes with it.
Summary
- '
KeyError: ... not in indexmeans the labels you asked for do not exist in the current Pandas index.' - Print the current columns or index at the failure point before guessing.
- Train-test feature drift after dummy encoding is a very common source of this error.
- Use
align,reindex, or scikit-learn pipelines to keep feature sets consistent. - Be clear about whether you are selecting by label with
.locor by position with.iloc.

