Home > database >  How to extract number from a string with regex
How to extract number from a string with regex

Time:03-18

I have a data-frame, with a column text containing strings like "bla bla bla Qwerty ### X/6 bla bla bla".

I want to first of all filter the data-frame and remain only with rows containing "Qwerty ### X/6". (It's fine if it also contain "bla bla" like in the example above.

Second, to create a new column that will have the number X extracted from the string in column text.

CodePudding user response:

Try this:

filtered_df = df[
    df.text.str.contains('Qwerty ### \d/6', regex=True)
].copy()

filtered_df['number'] = filtered_df.text.str.extract(
    'Qwerty ### (\d )/6'
)
  • Related