Check if pandas column contains all elements from a list

后端 未结 7 2017
忘掉有多难
忘掉有多难 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:34

    IIUC, explode and a boolean filter

    the idea is to create a single series then we can groupby the index the count the true occurrences of your list using a cumulative sum

    s = frame['a'].str.split(',').explode().isin(letters).groupby(level=0).cumsum()
    
    print(s)
    
    0    1.0
    0    1.0
    0    2.0
    1    1.0
    1    2.0
    1    2.0
    2    0.0
    2    0.0
    2    0.0
    3    1.0
    3    1.0
    3    2.0
    

    frame.loc[s[s.ge(2)].index.unique()]
    
    out:
    
           a
    0  a,b,c
    1  a,c,f
    3  a,z,c
    

提交回复
热议问题