Error: ValueError: Lengths must match to compare w

2020-05-06 16:37发布

问题:

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?

回答1:

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"])]