Error handling for .applymap()
I was thinking about how to handle errors.
I was reviewing the pandas applymap() docs and found ignore_na but that’s not what I’m looking for.
This is the DataFrame
df
| SNP_1 | SNP_2 | SNP_3 |
|---|---|---|
| T:G | T:G | ACC:ACC |
| T:G | T:G | ACC:ACC |
| T:G | T:G | ACC:ACC |
dict_map = {'T:G': 'K'}
df = df.applymap(lambda x: dict_map[x])
KeyError: 'ACC:ACC'
I get an error, obviously. Actually, I didn't expect to find that in the dataframe. Now I want to get rid of the whole column.
The expected output would be a dictionary mapped dataframe without the df['SNP_3'] column. Is there a way to identify a column to remove while applying a dictionary map?
df
| SNP_1 | SNP_2 |
|---|---|
| K | K |
| K | K |
| K | K |