Create bool mask from filter results in Pandas [duplicate]

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]

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.

Leave a Comment