python Pandas : replace a str value of column in another str column with a special character

标签 python python-3.x string pandas

有一个如下所示的数据框。

   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/

相关文章:

python - 如果函数的参数是语句中的字符串,如何编写函数的参数?

python - 尝试将数组与用户输入匹配时出错

python-3.x - 在 Python 3 上使用 with 打开文件有什么好处?

c++ - 故障排除替换 C++

c - 使用strcpy时出现段错误?

Python:在将每一行与矩阵中的每一行进行比较后存储非零唯一行的索引

Python 请求证书验证在 Windows 上失败

python - 使用 Pulp 加速整数线性优化

python-3.x - 使用随机森林时 scikit 中的 "ValueError: max_features must be in (0, n_features] "

java - 有没有办法获得数组中元素出现的最高次数?