简体   繁体   中英

filter list dataframe by element

I have a list and dataframe (example below).

                                     0  1
0                ((test1, AA), (1, 1))  1
1                ((test2, BB), (1, 1))  2
2                ((test1, CC), (1, 1))  3
3                ((test1, DD), (2, 1))  8
4                ((test3, EE), (3, 1))  9

I need to filter out only data with first elements test1 AND 1 . Could you please help?

Expected output:

                                     0  1
0                ((test1, AA), (1, 1))  1
2                ((test1, CC), (1, 1))  3

You can use boolean indexing:

v =  df[0].apply(lambda i: i[0][0] == 'test1' and i[1][0] == 1)
df = df[v]
print(df)

Output

                       0  1
0  ((test1, AA), (1, 1))  1
2  ((test1, CC), (1, 1))  3

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