【问题标题】:How should I filter one dataframe by entries from another one in pandas with isin?我应该如何使用 isin 在 pandas 中的另一个条目中过滤一个数据框?
【发布时间】:2022-01-03 11:16:08
【问题描述】:

我有两个数据框(df1、df2)。列名和索引是相同的(列条目的差异)。此外, df2 只有 20 个条目(正如我所说,它们也存在于 df1 中)。 我想通过 df2 条目过滤 df1,但是当我尝试使用 isin 进行过滤时,没有任何反应。

df1.isin(df2) or df1.index.isin(df2.index)

请告诉我我做错了什么以及我该怎么做..

【问题讨论】:

  • 我相信您可以通过搜索找到答案。 df1.loc[df2.index] 怎么样?

标签: python pandas


【解决方案1】:

首先pandas 中的isin 函数返回booleans 的Dataframe,而不是您想要的结果。因此,您使用的cmds 不起作用是有道理的。

我确信关注 psot 会有所帮助

pandas - filter dataframe by another dataframe by row elements

【讨论】:

    【解决方案2】:

    如果您想使用 df2 中也存在的索引选择 df1 中的条目,您应该可以这样做:

    df1.loc[df2.index]
    

    或者如果你真的想使用 isin:

    df1[df1.index.isin(df2.index)]
    

    【讨论】:

    • 这么简单的方法,我什至没有尝试这样做..谢谢你..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2013-06-07
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多