本文介绍了从 pandas 中的过滤器结果创建防毒面具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在查询单列时如何创建掩码以过滤数据框:

I know how to create a mask to filter a dataframe when querying a single column:

import pandas as pd
import datetime
index = pd.date_range('2013-1-1',periods=100,freq='30Min')
data = pd.DataFrame(data=list(range(100)), columns=['value'], index=index)
data['value2'] = 'A'
data['value2'].loc[0:10] = 'B'

data

    value   value2
2013-01-01 00:00:00 0   B
2013-01-01 00:30:00 1   B
2013-01-01 01:00:00 2   B
2013-01-01 01:30:00 3   B
2013-01-01 02:00:00 4   B
2013-01-01 02:30:00 5   B
2013-01-01 03:00:00 6   B

我在这里使用一个简单的面具:

I use a simple mask here:

mask = data['value'] > 4
data[mask]
    value   value2
2013-01-01 02:30:00 5   B
2013-01-01 03:00:00 6   B
2013-01-01 03:30:00 7   B
2013-01-01 04:00:00 8   B
2013-01-01 04:30:00 9   B
2013-01-01 05:00:00 10  A

我的问题是如何创建具有多列的遮罩?因此,如果我这样做:

My question is how to create a mask with multiple columns? So if I do this:

data[data['value2'] == 'A' ][data['value'] > 4]

此过滤器符合我的预期,但是如何按照其他示例从中创建防毒面具?我已经为此提供了测试数据,但是我经常想在其他类型的数据上创建一个掩码,所以我需要寻找任何指针.

This filters as I would expect but how do I create a bool mask from this as per my other example? I have provided the test data for this but I often want to create a mask on other types of data so Im looking for any pointers please.

推荐答案

您的布尔掩码是布尔值(显然),因此您可以使用布尔运算.布尔运算符包括(但不限于)&|,它们可以基于与"运算或或"运算来组合掩码.在您的特定情况下,您需要执行与"运算.因此,您只需像这样写遮罩即可:

Your boolean masks are boolean (obviously) so you can use boolean operations on them. The boolean operators include (but are not limited to) &, | which can combine your masks based on either an 'and' operation or an 'or' operation. In your specific case, you need an 'and' operation. So you simply write your mask like so:

mask = (data['value2'] == 'A') & (data['value'] > 4)

这确保您选择的是同时满足两个条件的那些行.通过用|替换&,可以选择可以满足两个条件之一的那些行.您可以照常选择结果:

This ensures you are selecting those rows for which both conditions are simultaneously satisfied. By replacing the & with |, one can select those rows for which either of the two conditions can be satisfied. You can select your result as usual:

data[mask]


尽管ayhan在其评论中指出的问题的答案回答了这个问题,但我认为OP缺乏布尔运算的想法.


Although this question is answered by the answer to the question that ayhan points out in his comment, I thought that the OP was lacking the idea of boolean operations.

这篇关于从 pandas 中的过滤器结果创建防毒面具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:45