Skip to content

[FAQ Bot] NEW: How do I avoid KeyError: 'converted' when re-running a cell that delet - #471

Open
github-actions[bot] wants to merge 1 commit into
mainfrom
faq-bot/issue-470
Open

github-actions[bot] wants to merge 1 commit into
mainfrom
faq-bot/issue-470

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

✨ FAQ NEW

Course: machine-learning-zoomcamp
Section: misc (This is a notebook/Jupyter rerun troubleshooting issue tied to the dataset split and target handling; it fits best in the cross-cutting notebook troubleshooting section.)
Related Issue: #470

Question

How do I avoid KeyError: 'converted' when re-running a cell that deletes the target column in pandas?

Decision Rationale

The repository does not yet have an FAQ specifically about avoiding KeyError: 'converted' when re-running a notebook cell after deleting the target column. The closest existing FAQ covers whether the target should remain in feature dataframes, not the rerun/delete idempotency issue.

Placement Details

  • Section ID: misc
  • Sort Order: End of section
  • Filename Slug: avoid-keyerror-converted-rerun-delete-target

🤖 Generated by FAQ Bot

Closes #470

@saeidsoheily

Copy link
Copy Markdown

You can use a safer approach to delete the columns such as:

df_train = df_train.drop(columns=["converted"], errors="ignore")
df_val = df_val.drop(columns=["converted"], errors="ignore")
df_test = df_test.drop(columns=["converted"], errors="ignore")

With errors="ignore", pandas will remove the column if it exists and do nothing if it has already been removed.

Another good notebook pattern is to avoid modifying the original split DataFrames directly. For instance:

X_train = df_train.drop(columns=["converted"])
y_train = df_train["converted"]

This keeps df_train unchanged, so re-running cells is less likely to cause problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FAQ] How to avoid KeyError when re-running target deletion cells in Jupyter Notebooks

1 participant