1

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]
Kong
  • 1,984
  • 8
  • 22
  • 45

3 Answers3

3

Using str

df_pos = df[df["label"].str[-1] == 1]
BENY
  • 296,997
  • 19
  • 147
  • 204
0

Make a new column:

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

and then do the operations on the column label2

hacker315
  • 1,759
  • 1
  • 12
  • 19
0

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.

Sindhukumari P
  • 178
  • 1
  • 4