【问题标题】:Pandas find every row in df1 where df2 matches either columnPandas 在 df1 中找到 df2 与任一列匹配的每一行
【发布时间】:2019-09-09 15:44:52
【问题描述】:

我有 2 个熊猫数据框。我想使用 df2 中的值并在 df1 中找到匹配的整行。

df1

   col1   col2
0  55     66
1  12     25 
2  18     22 
3  52     15
4  15     30
5  16     17
6  17     30
7  31     20

df2

   duplicates
0  15
1  17

我想使用 df2 中的值并遍历 df1 并接收和输出如下:

df3

   col1   col2
3  52     15
4  15     30
5  16     17
6  17     30



【问题讨论】:

    标签: python pandas


    【解决方案1】:

    isinany 一起使用

    df1[df1.isin(df2.duplicates.values).any(1)]
    Out[26]: 
       col1  col2
    3    52    15
    4    15    30
    5    16    17
    6    17    30
    

    【讨论】:

      【解决方案2】:

      您可以使用isinany 来检查给定行中是否有任何列是True

      df1[df1.isin(df2.duplicates.values).any(1)]
      
          col1  col2
      3    52    15
      4    15    30
      5    16    17
      6    17    30
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        相关资源
        最近更新 更多