Exception handling in the Pandas.apply () function

If I have a DataFrame:

myDF = DataFrame(data=[[11,11],[22,'2A'],[33,33]], columns = ['A','B'])

Gives the next data frame (starting with stackoverflow and not having enough reputation for a DataFrame image)

   | A  | B  |

0  | 11 | 11 |

1  | 22 | 2A |

2  | 33 | 33 |

If I want to convert column B values ​​to int and drop values ​​that cannot be converted, I have to do:

def convertToInt(cell):
    try:
        return int(cell)
    except:
        return None
myDF['B'] = myDF['B'].apply(convertToInt)

If I just do:

myDF ['B']. Apply (integer)

obviously the error is:

C: \ WinPython-32bit-2.7.5.3 \ python-2.7.5 \ lib \ site-packages \ pandas \ lib.pyd in pandas.lib.map_infer (pandas \ lib.c: 42840) ()

ValueError: invalid literal for int () with base 10: '2A'

Is there a way to add exception handling to myDF ['B']. apply ()

Thank you in advance!

+4
source share
3

/:

In [1]: myDF = DataFrame(data=[[11,11],[22,'2A'],[33,33]], columns = ['A','B'])

In [2]: myDF.convert_objects(convert_numeric=True)
Out[2]: 
    A   B
0  11  11
1  22 NaN
2  33  33

[3 rows x 2 columns]

In [3]: myDF.convert_objects(convert_numeric=True).dtypes
Out[3]: 
A      int64
B    float64
dtype: object

. coerce , nan -, .

, , , .

+7

lambda:

myDF['B'].apply(lambda x: int(x) if str(x).isdigit() else None)

:

>>> myDF
    A   B
0  11  11
1  22  2A
2  33  33

[3 rows x 2 columns]

>>> myDF['B'].apply(lambda x: int(x) if str(x).isdigit() else None)
0    11
1   NaN
2    33
Name: B, dtype: float64
+7

, , , (.. - , isdigit).

, try/except . , - .

import pandas as pd
import numpy as np

x=pd.DataFrame(np.array([['a','a'], [1,2]]))

def augment(x):
    try:
        return int(x)+1
    except:
        return 'error:' + str(x)

x[0].apply(lambda x: augment(x))
+1

Source: https://habr.com/ru/post/1534905/


All Articles