Search code examples
pythonlistpandascontainsmembership

membership test in pandas data frame column


I have a pandas data frame that one of its columns is a list. Please see below:

>>> a.head
    C1  C2
0   23  [2,4,5,8,1]
1   24  [1,2]
2   15  [-2]
3   19  [1,3,4,5,6,7,8,9,0]

I would like to find a row that contains 6 in the C2 and return the value in C1. I thought for something like

b = a["C1"][(6 in a["C2"])]
return(int(b))

but it is not working. Thanks.


Solution

  • I think you need apply and in for test value in list for creating boolean mask:

    print (a.C2.apply(lambda x: 6 in x))
    0    False
    1    False
    2    False
    3     True
    Name: C2, dtype: bool
    

    Then use loc with boolean indexing for select by mask:

    b = a.loc[a.C2.apply(lambda x: 6 in x), 'C1']
    print (b)
    3    19
    Name: C1, dtype: int64
    

    Last if need scalar output convert to numpy array and select first value by []:

    print (b.values)
    [19]
    
    print (b.values[0])
    19
    

    Or use iat or iloc:

    print (b.iat[0])
    19
    
    print (b.iloc[0])
    19