Pandas Extract Number from String

Given the following data frame:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A':['1a',np.nan,'10a','100b','0b'],
                   })
df

    A
0   1a
1   NaN
2   10a
3   100b
4   0b

I’d like to extract the numbers from each cell (where they exist).
The desired result is:

    A
0   1
1   NaN
2   10
3   100
4   0

I know it can be done with str.extract, but I’m not sure how.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Give it a regex capture group:

df.A.str.extract('(d+)')

Gives you:

0      1
1    NaN
2     10
3    100
4      0
Name: A, dtype: object

Method 2

To answer @Steven G ‘s question in the comment above, this should work:

df.A.str.extract('(^d*)')

Method 3

U can replace your column with your result using “assign” function:

df = df.assign(A = lambda x: x['A'].str.extract('(d+)'))


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x