Check if pandas column contains all elements from a list

后端 未结 7 1921
忘掉有多难
忘掉有多难 2020-12-09 03:14

I have a df like this:

frame = pd.DataFrame({\'a\' : [\'a,b,c\', \'a,c,f\', \'b,d,f\',\'a,z,c\']})

And a list of items:

let         


        
相关标签:
7条回答
  • 2020-12-09 03:54

    Use set.issubset:

    frame = pd.DataFrame({'a' : ['a,b,c', 'a,c,f', 'b,d,f','a,z,c','x,y']})
    letters = ['a','c']
    
    frame[frame['a'].apply(lambda x: set(letters).issubset(x))]
    
    Out:
    
           a
    0  a,b,c
    1  a,c,f
    3  a,z,c
    
    0 讨论(0)
提交回复
热议问题