1

I am new to pandas, I want to know that does pandas dataframe have their own way of exception handling other than using try/ except python.

I have tried exec function of python to write entire try/except in one line but I want pandas specific syntax or way of exception handling that can be done in a single line.

Below is the code that I have tried:

import pandas as pd

import numpy as np

data1 = {'id'   : [1,2,3,4,5],
         'Rate' : [5,9,3,'A',6],
         'Name' : ['a','b','c','d','e']}

df = pd.DataFrame(data1)

df['M_Rate1'] = df['Rate'].apply(lambda x, y=exec("def f(s):\n try:\n  return int(s) * 2\n except ValueError as e:  return 'Only Number can be converted to int'"): f(x))

Is their a better way for exception handling in oneline in pandas?

5
  • This question is off-topic here. Can you provide a minimal reproducible example example of what you're doing? Commented Aug 5, 2019 at 7:31
  • 1
    It's this way I have tried single line exception handling. I am Hopping for better way of exception handling than above, that can be done for dataframe . Commented Aug 5, 2019 at 9:05
  • 2
    Why does it have to fit on one line? That seems like a very unnecessary requirement... Commented Aug 5, 2019 at 9:57
  • My actual code length is too long i.e. about 3000 line, so to reduce it code complexity i was looking for it. Commented Aug 5, 2019 at 10:24
  • 1
    Complexity is more about readability than actual lines of code. It also obviously involves other concepts, but that's out of context here. Please, read the Zen of Python -- and focus on these 2 sentences : "Sparse is better than dense. Readability counts." Commented Aug 5, 2019 at 11:32

2 Answers 2

2

It looks like you are trying to apply a custom function in a very bad way, using a lambda, with a function defined using eval within an optional parameter.

You should try and go for something like this:

import pandas as pd

data = {
    'id'   : [1,2,3,4,5],
    'Rate' : [5,9,3,'A',6],
    'Name' : ['a','b','c','d','e']
}
df = pd.DataFrame(data)

def f(x):
    try:
        return int(s) * 2
    except ValueError as e:
        return 'Only Number can be converted to int'

df['M_Rate1'] = df['Rate'].apply(f)
print(df)
#   id Rate Name                              M_Rate1
#0   1    5    a                                   10
#1   2    9    b                                   18
#2   3    3    c                                    6
#3   4    A    d  Only Number can be converted to int
#4   5    6    e                                   12
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your solution but i am looking for single line way to deal with the exception.
1

Use pandas to_numeric and coerce failed conversions:

df['M_Rate1'] = pd.to_numeric(df['Rate'], errors='coerce') * 2

And if you must have an error message (not recommended):

df['M_Rate1'] = pd.to_numeric(df['Rate'], errors='coerce').mul(2).fillna('error_message')

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.