PyBoss
PyBoss

Reputation: 631

Python extract number between two special character in dataframe

I try to extract the number between the $ and white space in a column, then use the number to create a new column

df = pd.DataFrame({ 'name':['The car is selling at $15 dollars','he chair is selling at $20 dollars']})

I look at many solutions on stackoverflow about Regular expression. it's hard to understand

my code doesn't work

df['money'] = df['name'].str.extract(r'$\s*([^\.]*)\s*\.')

are there any other solutions besides RegEx, if not, how to fix my code?

Upvotes: 1

Views: 467

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195543

Escape the $:

df["money"] = df["name"].str.extract(r"\$(\d+\.?\d*)")
print(df)

Prints:

                                 name money
0   The car is selling at $15 dollars    15
1  he chair is selling at $20 dollars    20

Upvotes: 1

Related Questions