错误:ValueError:尝试过滤Pandas中的数据帧时,长度必须匹配才能进行比较 [英] Error: ValueError: Lengths must match to compare when trying to filter the dataframe in Pandas

查看:1111
本文介绍了错误:ValueError:尝试过滤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.

我有一个dataframe,具有这种性质

I have a dataframe, something of this nature

df

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

我正在尝试过滤dataframe:

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

但是出现以下错误:ValueError: Lengths must match to compare

请问我该如何解决这个问题?

Please how can I solve this issue?

推荐答案

使用列表推导进行过滤:

Use list comprehension for filtering:

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

或比较tuple s:

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

这篇关于错误:ValueError:尝试过滤Pandas中的数据帧时,长度必须匹配才能进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆