dhee
dhee

Reputation: 13

Can we do dataframe level exception handling for pandas?

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?

Upvotes: 1

Views: 7163

Answers (2)

IanS
IanS

Reputation: 16251

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')

Upvotes: 1

IMCoins
IMCoins

Reputation: 3306

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

Upvotes: 2

Related Questions