'Python pandas applymap error handling for unexpected value
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 |
Solution 1:[1]
Here's one way to do it. First, fill the non mappable column with NaN then drop them:
df = df.applymap(lambda x: dict_map[x] if x in dict_map else pd.NA)
df = df.dropna(axis=1, how='all')
Output:
SNP_1 SNP_2
0 K K
1 K K
2 K K
Solution 2:[2]
i don't know. it can help solved your problem.
dict_map = { 'T:G': 'K'}
df = df.applymap(lambda x: dict_map[x] if x in dict_map else "")
| SNP_1 | SNP_2 | SNP_3 |
|---|---|---|
| K | k | |
| K | k | |
| K | k |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhyuday Vaish |
| Solution 2 | kunnapat |
