You need to enable JavaScript to run this app. 如何根据Comments列是否包含Verizon创建布尔型True/False新列
优惠活动
大模型
产品
解决方案
定价
更多

如何根据Comments列是否包含Verizon创建布尔型True/False新列

实现Comments列包含Verizon的标记逻辑

直接用pandas内置的字符串向量化方法即可,不需要逐行循环处理,效率更高。

  1. 首先构造和示例一致的原始DataFrame:
import pandas as pd

df = pd.DataFrame({
    "Comments": [
        "Yes - Verizon",
        "Verizon",
        "",
        "Does not contain ",
        "T-Mobile",
        "Standby, call for more",
        "Verizon Software"
    ]
})
  1. 执行匹配逻辑,新增标记列:
df["Has Verizon?"] = df["Comments"].str.contains("Verizon", na=False)

运行后得到的结果和预期完全一致:

Comments  Has Verizon?
0          Yes - Verizon          True
1                Verizon          True
2                                 False
3       Does not contain          False
4                T-Mobile         False
5  Standby, call for more         False
6       Verizon Software          True

说明

  • 参数na=False会对空字符串、缺失值等无有效文本的行直接返回False,避免出现空值干扰结果
  • 默认匹配是区分大小写的,如果需要同时识别小写、全大写的同类写法(比如verizon、VERIZON),可以给方法传入case=False参数,即:
df["Has Verizon?"] = df["Comments"].str.contains("Verizon", case=False, na=False)

内容的提问来源于stack exchange,提问作者Chris90

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.18 09:36:11