【问题标题】:How to return a certain column value if substring is present in adjacent column that contains string? [closed]如果包含字符串的相邻列中存在子字符串,如何返回某个列值? [关闭]
【发布时间】:2020-05-28 14:51:40
【问题描述】:

我有一个无法解决的子字符串问题。假设我有 3 列:

df['Value_Desired'] = [1,2,3,4,..,n]
df['Substring'] = [yellow, blue, orange,...,n]
df['String'] = [xxyellowxx, something else, zz_orange_zz]

df['Desired'] = [1,3] 

由于黄色和橙色都是字符串列中的子字符串,我想将值 1 和 3 返回到所需的列中。

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: python regex string numpy substring


    【解决方案1】:

    给你:

    df = df[df.apply(lambda row: row['Substring'] in row['String'], axis=1)]
    print(df)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多