简体   繁体   中英

Extract number from alpha-numeric column pandas

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'

You can use .str.extract with regex pattern containing capturing group:

df['Value'] = df['Value'].str.extract(r'^(-?\d+)', expand=False).astype(int)

              Code   Value
0  USH0001108421891  -9999
1  USH0001108421892  -9999
2  USH0001108421893    -77
3  USH0001108421894    483
4  USH0001108421895    325
5  USH0001108421896    297

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM