Home > Blockchain >  Python pandas applymap error handling for unexpected value
Python pandas applymap error handling for unexpected value

Time:04-26

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

CodePudding user response:

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

CodePudding user response:

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
  • Related