Home > front end >  Regex: Extracting numbers only until first space
Regex: Extracting numbers only until first space

Time:10-06

I have below data:

#1314515 22-09-2021

And need to extract only numbers and not date as below:

1314515

Have tried below Regex combinations but does not work:

[\s0-9] 
\s[0-9] 

Please help with correct combination.

CodePudding user response:

Use regex and pd.Series.str.extract:

df['col'] = df['col'].str.extract('(\d )(?=\s)')

To convert to integer:

df['col'] = df['col'].str.extract('(\d )(?=\s)').astype(int)

Explanation.

CodePudding user response:

We can use str.extract as follows:

df["num"] = df["col"].str.extract(r'^#(\d )')
  • Related