python - Python:Pandas 在 Pandas (Dataframe) 中查找文本的可用性
问题描述
我在熊猫数据框中有两列 ColA ColB,如果 colA 包含与 colB 匹配的单词,我想将 ColB 与 ColA 进行比较,那么我必须将 colC 更新为可用。
If it not macthes print not available.
ColA ColB
You can extract_insights on product reception insights
user various sources like extract_insights etc. insights
some other sourced mail by using signals from state art text
注意:即使 A 列包含任何特殊字符,它仍然应该能够识别 colB 文本
期望的输出:
If it not macthes print not available.
ColA ColB Colc
You can extract_insights on product reception insights AVB
user various sources like extract_insights etc. insights AVB
some other sourced mail by using signals from state art text NAVB
解决方案
尝试以下操作:
import pandas as pd
# Initialize example dataframe
data = [
["You can extract_insights on product reception", "insights"],
["user various sources like extract_insights etc.", "insights"],
["some other sourced mail by using signals from state art", "text"],
]
df = pd.DataFrame(data=data, columns=["ColA", "ColB"])
# Create column C with comparison results
df["ColC"] = [
"AVB" if (b in a) else "NAVB"
for (a, b) in zip(df["ColA"], df["ColB"])
]
print(df)
# Output:
# ColA ColB ColC
# 0 You can extract_insights on product reception insights AVB
# 1 user various sources like extract_insights etc. insights AVB
# 2 some other sourced mail by using signals from ... text NAVB
推荐阅读
- azure - Azure 文件共享:无法映射网络驱动器
- asp.net-core - Asp Net Core Swashbuckle | 为动态子域模板 AuthorizationUrl 和 TokenUrl
- swift - Swift 代码仅在不应该运行时运行,即使它被封装在 if 语句中
- javascript - Javascript操作数组以返回值范围
- c# - 如何使用机器学习预测vertor单值
- python - 在 Python 中向字典中添加新的键值对的时间复杂度是多少?
- mysql - MySQL 中布尔全文搜索中的`~`(波浪号)运算符的行为与 MySQL 开发人员网站中所述不同
- sql - SSMS 打开查询 SQL 命令未正确结束
- javascript - 如何使用 JavaScript (PWA) 在 Android 设备上为 FCM 创建 channel_ID?
- unit-testing - 使用 Waffle 读取合同余额?