有一个如下数据框。

   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


ž
我当前的代码如下

df_dev['text'].str.replace(df_dev['num'], 'UNK')


并且有错误:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

最佳答案

让我们使用regexreplace

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 - python pandas:用特殊字符替换另一个str列中该列的str值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53939222/

10-11 20:29
查看更多