I try to extract the number between the $ and white space in a column, then use the number to create a new column
JavaScript
x
2
1
df = pd.DataFrame({ 'name':['The car is selling at $15 dollars','he chair is selling at $20 dollars']})
2
I look at many solutions on stackoverflow about Regular expression. it’s hard to understand
my code doesn’t work
JavaScript
1
2
1
df['money'] = df['name'].str.extract(r'$s*([^.]*)s*.')
2
are there any other solutions besides RegEx, if not, how to fix my code?
Advertisement
Answer
Escape the $
:
JavaScript
1
3
1
df["money"] = df["name"].str.extract(r"$(d+.?d*)")
2
print(df)
3
Prints:
JavaScript
1
4
1
name money
2
0 The car is selling at $15 dollars 15
3
1 he chair is selling at $20 dollars 20
4