pandas - filter dataframe by another dataframe by row elements

前端 未结 6 1210
后悔当初
后悔当初 2020-11-27 14:12

I have a dataframe df1 which looks like:

   c  k  l
0  A  1  a
1  A  2  b
2  B  2  a
3  C  2  a
4  C  2  d

and another called

6条回答
  •  没有蜡笔的小新
    2020-11-27 14:53

    Another option that avoids creating an extra column or doing a merge would be to do a groupby on df2 to get the distinct (c, l) pairs and then just filter df1 using that.

    gb = df2.groupby(("c", "l")).groups
    df1[[p not in gb for p in zip(df1['c'], df1['l'])]]]
    

    For this small example, it actually seems to run a bit faster than the pandas-based approach (666 µs vs. 1.76 ms on my machine), but I suspect it could be slower on larger examples since it's dropping into pure Python.

提交回复
热议问题