【问题标题】:Match exact string in dataframe column匹配数据框列中的确切字符串
【发布时间】:2020-10-23 08:05:19
【问题描述】:

如何在数据框列中找到完全匹配的字符串?

目前,我正在 col_8 中搜索字符串 '1',它为第 2 行返回 True,但我希望它为 false,因为它实际上是 12。提前致谢!

df['new'] = np.where(df['col_8'].str.contains('1'),1,0)
print(df)

    col_8  col_9  new
0    1,2,3  7     1
1    1,3,8  10    1
2    12     8     1
3    9      3     0

【问题讨论】:

  • 尝试拆分,df.col_8.apply(lambda x : 1 if "1" in x.split(",") else 0)

标签: python numpy dataframe string-matching


【解决方案1】:

这样就可以了

df['new'] = np.where(df['col_8'].str.contains('1,'),1,0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2021-05-30
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多