简体   繁体   中英

pandas dataframe search column of by list

Im trying to filter a pandas dataframe containing rows of lists by a list. A minimal example is:

>>>import pandas as pd
>>>d={'a':[[1,2],[3,4]], 'b':[['f1','f2'],['f3','f4']]}
>>>ds = pd.DataFrame(d)
>>>ds
        a         b
0  [1, 2]  [f1, f2]
1  [3, 4]  [f3, f4]

>>> ds.a == [1,2]
0    False
1    False
Name: a, dtype: bool

What I'm looking for is a method to get:

    ds.a == [1,2]
    0    True
    1    False

Use:

d={'a':[[2,1],[3,4],[1,2]], 'b':[['f1','f2'],['f3','f4'],['f5','f6']]}
ds = pd.DataFrame(d)
print (ds)
        a         b
0  [2, 1]  [f1, f2]
1  [3, 4]  [f3, f4]
2  [1, 2]  [f5, f6]

#not important order - compare set
print (ds.a.apply(set) == set([1,2]))
0     True
1    False
2     True
Name: a, dtype: bool

#important order - compare tuples
print (ds.a.apply(tuple) == tuple([1,2]))
0    False
1    False
2     True
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