与列表的Pandas DataFrame列的比较 [英] Comparisons with Pandas DataFrame columns of lists

查看:128
本文介绍了与列表的Pandas DataFrame列的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框df:

I have a dataframe df like this:

col1 | col2
 a   | [1,2]
 b   | [3,4]
 c   | [3,9]

我想基于匹配的输入数组获取行,所以如果我有数组[1,2],我可以得到:

I want to get the row based on a matching input array, so if I have the array [1,2], I can get:

col1 | col2
 a   | [1,2]

当我尝试使用此公式执行此操作时,它不起作用:

When I try to do this using this formula, it doesn't work:

df.loc[df['Col2'] == [1,2]]
Error: Lengths must match to compare

推荐答案

真正的错误原因是并非所有列表的大小都相同,这会导致DataFrame.eq出现问题.

The real cause of your error was that not all lists are of the same size, this causes issues with DataFrame.eq.

解决此问题的最佳方法是使用列表推导构建一个布尔掩码,然后使用它索引到df:

The best approach to tackling this is to build a boolean mask using a list comprehension and then use it to index into df:

df[[v == [1, 2] for v in df['col2'].tolist()]]

另一种替代方法是df.apply,但这并不比这快.

Another alternative is df.apply, but that isn't nearly as fast as this.

这篇关于与列表的Pandas DataFrame列的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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