python - 我有一个混合整数字符串列:如何只更改字符串?
问题描述
我有一个数据框,其中有一列名为Text
. 该列的行均采用以下格式:
xxx - some sentence
其中xxx
是一个随机数。我所拥有的一个例子是:
Text
100 - Hello World
200 - Bye World
300 - Good World
我希望 python 仅查找字符串字符(“某些句子”)并将其替换为我指定的值。我目前使用的方法是:
mapping = {"100 - Hello World":"100 - Bonjour Le Monde"}
df = df.replace({"Text":mapping})
这适用于小型数据集,但该数据集有 15k+ 个条目和多个随机数。我宁愿不必每次都指定每个数字。如何告诉 python 找到字符串并只翻译字符串?
非常非常感谢你!
解决方案
regex=True
mapping = {"Hello World": "Bonjour Le Monde"}
df.replace({"Text":mapping}, regex=True)
Text
0 100 - Bonjour Le Monde
1 200 - Bye World
2 300 - Good World
推荐阅读
- java - 如何使用 Java 在 heroku/docker 中创建临时目录?
- php - PHP soapClient 发送 XML 数据
- c# - 错误(条件:amqp:内部错误,描述:从流中读取时出现异常)
- c++ - 链接二叉搜索树的标准差
- android - 我需要在 Android 上使用 moshi 解析完整的 json 吗?
- python - 您如何将数据从 main.qml 传递到 main.py,然后将其发送到其他 qml 文件
- conditional-statements - 使用 kiefunctions 在独占网关上检查多个条件的语法
- google-cloud-platform - 在 bigquery 中将 'dd-mmm-yy' 转换为 '%Y-%m-%d %H:%M:%S' ;
- php - 需要帮助解决未定义的数组键
- python - 如何使用 BeautifulSoup4 从 Python 网站中获取经常更新的 .php 文本?