Python提取数据帧中两个特殊字符之间的数字



我尝试提取列中$空白之间的数字,然后使用该数字创建新列

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

我在stackoverflow上看到了许多关于正则表达式的解决方案。很难理解

我的代码不起作用

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

除了RegEx,还有其他解决方案吗?如果没有,如何修复我的代码?

逃离$:

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

打印:

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

相关内容

最新更新