Skip to content
Advertisement

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?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement