【问题标题】:Comparing common strings in two pandas dataframe columns比较两个熊猫数据框列中的常见字符串
【发布时间】:2015-05-15 13:36:51
【问题描述】:

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

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

我想创建一个新列来标记 conname1 中的字符串是否包含在 connames 中。因此,从上面的示例中,数据框现在将是:

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

【问题讨论】:

  • 您的问题是什么(如带问号的句子中)?你只陈述你想要的。实现这个有问题吗。你想聘请程序员吗?您是否遇到了错误问题(如果有,是哪一个)。这是一个面向程序员的网站,因此包含一些代码或多或少是强制性的。

标签: python pandas


【解决方案1】:

设置框架:

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)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 2020-02-19
相关资源
最近更新 更多