有一个如下所示的数据框。
id num text
1 1.2 price is 1.2
1 2.3 price is 1.2 or 2.3
2 3 The total value is $3 and $130
3 5 The apple value is 5dollar and $150
我想用字符“UNK”替换文本中的数字
新的数据框更改为:
id num text
1 1.2 price is UNK
1 2.3 price is 1.2 or UNK
2 3 The total value is UNK and 130
3 5 The apple value is UNK dollar and $150
z 我当前的代码如下
df_dev['text'].str.replace(df_dev['num'], 'UNK')
并且有错误:
TypeError: 'Series' objects are mutable, thus they cannot be hashed
最佳答案
让我们使用正则表达式
和替换
df.text.replace(regex=r'(?i)'+ df.num.astype(str),value="UNK")
0 price is UNK
1 price is 1.2 or UNK
2 The total value is UNK
Name: text, dtype: object
#df.text=df.text.replace(regex=r'(?i)'+ df.num.astype(str),value="UNK")
更新
(df.text+' ').replace(regex=r'(?i) '+ df.num.astype(str)+' ',value=" UNK ")
0 price is UNK
1 price is 1.2 or UNK
2 The total value is UNK and 130
Name: text, dtype: object
关于 python Pandas : replace a str value of column in another str column with a special character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53939222/