daiyue
daiyue

Reputation: 7448

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

I have a df,

inv_id
d5
dd

a123

I like to convert inv_id into int and checks if its values is < 100,

df['inv_id'].str.replace(r'\D+', '').apply(int) < 100

but i got

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

I understand that empty string cannot be converted into int, but I like to set empty strings as False no matter inv_id is empty string originally or after str.replace conversion into an empty string. so the results looks like,

inv_id    res
d5        True
dd        False
          False
a123      False

Upvotes: 2

Views: 3923

Answers (1)

BENY
BENY

Reputation: 323226

You may need using to_numeric rather than apply(int) ..

pd.to_numeric(df.inv_id.str.replace(r'\D+', ''),errors ='coerce')<100

Upvotes: 4

Related Questions