比较两个PANDAS数据框列中的常见字符串



我有一个熊猫数据框,如下:

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

我想创建一个新列,该列标记Coname1中的字符串是否包含在conames中。因此,从上面的示例中,数据帧现在为:

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

设置帧:

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

尝试以下操作:

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

最新更新