简体   繁体   中英

pandas: identifying if a first n characters are letter followed by numbers

I have a dataframe with column which contains multiple reference values. I am trying to filter a ceratin group of references which follow this format:

ABCD12345678

Basically the first 4 characters are letters followed by 8 numbers.

I tried:

df_new=df[df['col'].str.match('[a-zA-Z]', na = False)]

and

bew_2=df[df['col'].str.slice(0,4).str.contains('[a2-3]', na = False)]

But neither worked. It would be great if someone could guide me through this.

I think you can use

m = df['col'].str.match('^\w+\d+$', na = False)

# if the number is fixed

m = df['col'].str.match('^\w{4}\d{8}$', na = False)
print(m)

0    True
Name: col, dtype: bool

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