3

I am new to pandas and I have a simple dataframe and want to extract certain rows based on a column. However, the type in this column is a list.

Example:

df = pd.DataFrame([['text1', [1,2,3]], ['text2', [2,3,4]]], columns=['text','list_value'])

The data frame looks like:

    text    list_value
0   text1   [1, 2, 3]
1   text2   [2, 3, 4]

I tried

df.loc[df['list_value'] == [1,2,3]]

And it returns an error :

ValueError: Arrays were different lengths: 2 vs 3

I wonder if there is any better solution than using for loop to iterate the dataframe.

Similar question but the solution is not work for me: Select rows from a DataFrame based on values in a column in pandas.

Leoli
  • 719
  • 1
  • 9
  • 18

1 Answers1

3

You can adding apply tuple, when there is list in a cell , pandas sometime return the wired result

df.loc[df['list_value'].apply(tuple) == tuple([1,2,3])]
Out[58]: 
    text list_value
0  text1  [1, 2, 3]
BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    Great Thanks! but i cannot understand why the error is `Arrays were different lengths: 2 vs 3`. I guess 3 is the length of the list in the column, but what is 2? – Leoli May 11 '18 at 03:11