简体   繁体   English

与Pandas DataFrame列列表进行比较

[英]Comparisons with Pandas DataFrame columns of lists

I have a dataframe df like this: 我有一个像这样的数据帧:

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

I want to get the row based on a matching input array, so if I have the array [1,2], I can get: 我想基于匹配的输入数组获取行,所以如果我有数组[1,2],我可以得到:

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

The real cause of your error was that not all lists are of the same size, this causes issues with DataFrame.eq . 您的错误的真正原因是并非所有列表都具有相同的大小,这会导致DataFrame.eq出现问题。

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

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

Another alternative is df.apply , but that isn't nearly as fast as this. 另一个替代方案是df.apply ,但这并不像这个快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM