简体   繁体   中英

pandas regex flag if pattern matched

I have a string column in a pandas dataframe.

I want to flag another column with 0/1 if the following pattern is matched in the first column.

A typical value for the column would look like this:

"b064571d-9d72-4225-8ccf-5528622c5680"

8x - 4x - 4x - 4x - 12x

where x is a character string followed by hyphen (where the character is alphanumeric and may be upper or lower case)

Do I use np.where and a regex match - not sure what this would be?

Many thanks

I have put together my best attempt - but not sure of the regex syntax to match the pattern, with the hyphen separator

8x - 4x - 4x - 4x - 12x

s1 = pd.Series(['b064571d-9d72-4225-8ccf-5528622c5680', 'dog', 'house 
and parrot', '23', np.NaN])

s1.str.contains('[^a-zA-Z0-9]', regex=True)

The pattern may look like this:

s1.str.contains(r'[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}', regex=True)

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