0

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
0

2 Answers 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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.