JA-pythonista
JA-pythonista

Reputation: 1323

Error: ValueError: Lengths must match to compare when trying to filter the dataframe in Pandas

I am so sorry for asking this, but for some reasons, I have spent some minutes figuring this out but I am not getting it.

I have a dataframe, something of this nature

df

Output:
  TypePro
  ["JJ", "KK"]
  ["JK", "RJ"]
  ["JK"]
  ["JK"]    

I am trying to filter the dataframe:

df_JJ_KK = df[df.TypePro == ["JJ", "KK"]]

But I get the following error: ValueError: Lengths must match to compare

Please how can I solve this issue?

Upvotes: 1

Views: 473

Answers (1)

jezrael
jezrael

Reputation: 862481

Use list comprehension for filtering:

df = df[[x == ["JJ", "KK"] for x in df.TypePro]]
print (df)
    TypePro
0  [JJ, KK]

Or compare tuples:

df = df[df.TypePro.map(tuple) == tuple(["JJ", "KK"])]

Upvotes: 1

Related Questions