简体   繁体   中英

Pandas - Select rows from a dataframe based on list in a column

This thread Select rows from a DataFrame based on values in a column in pandas shows how you can select rows if the column contains a scalar. How can I do so if my column contains a list of varied length?

To make it simple, assume the values in the list are similar.

           label
0          [1]
1          [1]
2          [1]
3          [1]
4          [1]
5          [1]
6          [1]
7          [1]
8          [1]
9          [1]
10         [1]
11         [1]
12         [1]
13         [1]
14         [1]
15         [1]
16         [0,0,0]
17         [1]
18         [1]
19         [1]
20         [1]
21         [1]
22         [1]
23         [1]
24         [1]
25         [1]
26         [1]
27      [1, 1]
28      [1, 1]
29      [0, 0]

I tried the following which does not work. What I tried was to check if the last element of the list is equivalent to a scalar.

df_pos = df[df["label"][-1] == 1]

Using tolist()[-1] returns me only the last row.

df_pos = df[df["label"].tolist()[-1] == 1]

使用str

df_pos = df[df["label"].str[-1] == 1]

Make a new column:

df['label2'] = df['label'].apply(lambda x: x[0])

and then do the operations on the column label2

Today, I have worked on a similar problem where I need to fetch rows containing a value we are looking for, but the data frame's columns' values are in the list format.

This is the solution I have come up with.

fetched_rows = df.loc [ df['column_name'].map( lambda x: True if check_element in x else False) == True ]

Where column_name ---> the column name that we need to look into

check_element ---> the value that we use to check whether it exists.

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