我有一个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
答案 0 :(得分:5)
设置框架:
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)