How to filter on pandas dataframe when column data type is a list

后端 未结 2 947
面向向阳花
面向向阳花 2021-01-20 18:31

I am having some trouble filtering a pandas dataframe on a column (let\'s call it column_1) whose data type is a list. Specifically, I want to return only rows such that co

2条回答
  •  生来不讨喜
    2021-01-20 19:35

    consider the pd.Series s

    s = pd.Series([[1, 2, 3], list('abcd'), [9, 8, 3], ['a', 4]])
    print(s)
    
    0       [1, 2, 3]
    1    [a, b, c, d]
    2       [9, 8, 3]
    3          [a, 4]
    dtype: object
    

    And a testing list test

    test = ['b', 3, 4]
    

    Apply a lambda function that converts each element of s to a set and intersection with test

    print(s.apply(lambda x: list(set(x).intersection(test))))
    
    0    [3]
    1    [b]
    2    [3]
    3    [4]
    dtype: object
    

    To use it as a mask, use bool instead of list

    s.apply(lambda x: bool(set(x).intersection(test)))
    
    0    True
    1    True
    2    True
    3    True
    dtype: bool
    

提交回复
热议问题