简体   繁体   中英

Pandas compare columns of list

I have columns in my DataFrame storing lists and I would like to compare each element in the column with a lists .

All the methods I have tried fails:

df.list_col == ['3', '4']
df.list_col.isin([['3', '4']])
df.list_col.equals(['3', '4'])

Is there a simple solution to this?

You can use apply with in :

df = pd.DataFrame({'A':[[1,2],[2,4],[3,1]],
                   'B':[4,5,6]})

print (df)
        A  B
0  [1, 2]  4
1  [2, 4]  5
2  [3, 1]  6

print (df.A.apply(lambda x: 2 in x))
0     True
1     True
2    False
Name: A, dtype: bool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM