pandas - filter dataframe by another dataframe by row elements

前端 未结 6 1201
后悔当初
后悔当初 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 15:07

    I think this is a quite simple approach when you want to filter a dataframe based on multiple columns from another dataframe or even based on a custom list.

    df1 = pd.DataFrame({'c': ['A', 'A', 'B', 'C', 'C'],
                        'k': [1, 2, 2, 2, 2],
                        'l': ['a', 'b', 'a', 'a', 'd']})
    df2 = pd.DataFrame({'c': ['A', 'C'],
                        'l': ['b', 'a']})
    
    #values of df2 columns 'c' and 'l' that will be used to filter df1
    idxs = list(zip(df2.c.values, df2.l.values)) #[('A', 'b'), ('C', 'a')]
    
    #so df1 is filtered based on the values present in columns c and l of df2 (idxs)
    df1 = df1[pd.Series(list(zip(df1.c, df1.l)), index=df1.index).isin(idxs)]
    

提交回复
热议问题