简体   繁体   English

比较两个pandas dataframe列中的常见字符串

[英]Comparing common strings in two pandas dataframe columns

I have a pandas data frame as follows: 我有一个pandas数据框如下:

coname1        coname2
Apple          [Microsoft, Apple, Google]
Yahoo          [American Express, Jet Blue]
Gap Inc       [American Eagle, Walmart, Gap Inc]

I want to create a new column that flags whether the string in coname1 is contained in conames. 我想创建一个新列,标记coname1中的字符串是否包含在conames中。 So, from the above example, the dataframe would now be: 因此,从上面的示例中,数据帧现在将是:

coname1        coname2                               isin
Apple          [Microsoft, Apple, Google]            True
Yahoo          [American Express, Jet Blue]          False
Gap Inc       [American Eagle, Walmart, Gap Inc]     True

set up frame: 设置框架:

df =pd.DataFrame({'coname1':['Apple','Yahoo','Gap Inc'],
          'coname2':[['Microsoft', 'Apple', 'Google'],['American Express', 'Jet Blue'],
                     ['American Eagle', 'Walmart', 'Gap Inc']]})

try this: 试试这个:

df['isin'] =df.apply(lambda row: row['coname1'] in row['coname2'],axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM