【问题标题】:Pandas: How to return the list of values which are not in another dataframe?Pandas:如何返回不在另一个数据框中的值列表?
【发布时间】:2020-06-29 17:36:01
【问题描述】:
import pandas as pd
df1 = pd.DataFrame({'name': ['CAD123', 'MXN789', 'EUR567','JPY224', 'EUR673', 'PLN254'], 
           'currency': ['CAD', 'MXN', 'EUR', 'JPY', 'EUR','PLN']})

df2 = pd.DataFrame({'currency':['EUR','PLN']})

以上是我的两个数据框。我正在做一些数据分析,并希望在另一个数据框中没有“货币”的“名称”列中的值列表。我的预期输出如下。请提出建议。

Expected_list = ['CAD123','MXN789','JPY224']

【问题讨论】:

  • 请提出建议。 有具体问题吗?你有没有尝试过,做过任何研究?请参阅How to Askhelp center

标签: python pandas


【解决方案1】:

我们使用isinloc 合作

l = df1.loc[~df1.currency.isin(df2.currency), 'name'].tolist()
['CAD123', 'MXN789', 'JPY224']

【讨论】:

  • 太……快……+1
  • 使用.loc和不使用df1.name[~df1.currency.isin(df2.currency)].tolist()有性能差异吗?
  • @TrentonMcKinney 最好不要把切片链起来~
  • YOBEN_S 感谢您的及时建议。我错过了〜否定。
【解决方案2】:

你可以试试这个:-

df1[~df1['currency'].isin(df2['currency'])]['name'].tolist()

输出:-

['CAD123', 'MXN789', 'JPY224']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2021-04-23
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多