正则表达式替换预期的字符串或字节像对象

时间:2018-05-16 09:20:33

标签: python regex pandas

我有the following code,我已经通过Pandas导入了一个数据集,并且我试图用逗号替换数字(例如,"12,000"),但我似乎总是遇到错误"TypeError: expected string or bytes-like object"

df = pd.read_csv("C:/Users/Dell/Downloads/osc_samples_without.csv")
df2=df.loc[:,['Id','Description']]
df['Description'] = df['Description'].apply(lambda x:re.sub(r'(?<=\d)[,\.]','', df2))

我是Python和Regex的新手,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

您可以直接使用replace而无需明确使用re

df2['Description'] = df2['Description'].str.replace(r'(?<=\d)[.,]', '')

下面,

  • (?<=\d) - 一个积极的lookbehind匹配紧接数字
  • 前面的位置
  • [.,] - 匹配.,