Extract number from alpha-numeric column pandas

  dataframe, pandas, python

Input df

Code Value

USH0001108421891  -9999    
USH0001108421892  -9999 X3 
USH0001108421893  -77EX3 
USH0001108421894   483EQ3 
USH0001108421895   325EX3 
USH0001108421896   297ES3 

As can be seen from the example, the column Value has both strings and integers. But I want the only the first set of integers before the alphabets.

Expected df

Code               Value
USH0001108421891  -9999    
USH0001108421892  -9999 
USH0001108421893  -77
USH0001108421894   483
USH0001108421895   325
USH0001108421896   297 

I tried this, but it returned an error .

df1['Value'] = df1['Value'].astype(int)
ValueError: invalid literal for int() with base 10: '-77EX3'

Source: Python Questions

One Reply to “Extract number from alpha-numeric column pandas”

LEAVE A COMMENT