【问题标题】:In Python/Pandas, Check if a comma separated string contains any value in a list在 Python/Pandas 中,检查逗号分隔的字符串是否包含列表中的任何值
【发布时间】:2019-06-11 02:22:35
【问题描述】:

我在 pandas 数据框中有一列如下所示:

Code
----
ABC,DEF,XYZ
ABC,XYZ
...
...
CBA,FED,ABC

我正在尝试检查这一系列逗号分隔的字符串是否包含以下列表中的任何字符串:

["UVW","XYZ"]

我知道我们可以在 df["Code"] 中检查像“XYZ”这样的单个值,但是我们如何才能在 Python 中检查值列表,或者 Pandas 有什么特殊功能?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    pd.Series.str.containsregex=True 一起使用:

    给定Seriess 和目标列表l

    s 
    0    ABC,DEF,XYZ
    1        ABC,XYZ
    2    CBA,FED,ABC
    
    l = ["UVW","XYZ"]
    
    s.str.contains('|'.join(l))
    

    输出:

    0     True
    1     True
    2    False
    dtype: bool
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多