logical or on list of pandas masks

2024/10/12 22:32:37

I have a list of boolean masks obtained by applying different search criteria to a dataframe. Here is an example list containing 4 masks:

mask_list = [mask1, mask2, mask3, mask4]

I would like to find the logical or of all of the masks in the list. In other words,

or_mask = mask_list[0] | mask_list[1] | mask_list[2] | mask_list[3]

Is there a compact way to accomplish this for a list containing an arbitrary number of masks? I understand that I can write a for loop as below, but is there a shorter, more pythonic way to do this?

for i in range(len(mask_list)):if i == 0:temp_mask_or = mask_list[i]else:temp_mask_or = temp_mask_or | mask_list[i]
Answer

You can use reduce:

or_(x,y) means x|y so this will work:

from operator import or_
or_mask = reduce(or_,mask_list)

Edit: As suggested by JoeCondron, instead of operator.or_ you could use numpy.logical_or which gives the same result but is faster.

https://en.xdnf.cn/q/69599.html

Related Q&A

How to view the implementation of pythons built-in functions in pycharm?

When I try to view the built-in function all() in PyCharm, I could just see "pass" in the function body. How to view the actual implementation so that I could know what exactly the built-in f…

How to gracefully fallback to `NaN` value while reading integers from a CSV with Pandas?

While using read_csv with Pandas, if i want a given column to be converted to a type, a malformed value will interrupt the whole operation, without an indication about the offending value.For example, …

Python - object layout

can somebody describe the following exception? What is the "object layout" and how it is defined? ThanksTraceback (most recent call last):File "test_gui.py", line 5, in <module…

Using Tor proxy with scrapy

I need help setting up Tor in Ubuntu and to use it within scrapy framework.I did some research and found out this guide:class RetryChangeProxyMiddleware(RetryMiddleware):def _retry(self, request, reaso…

Best practice for structuring module exceptions in Python3

Suppose I have a project with a folder structure like so./project__init__.pymain.py/__helpers__init__.pyhelpers.py...The module helpers.py defines some exception and contains some method that raises th…

How can you read a gzipped parquet file in Python

I need to open a gzipped file, that has a parquet file inside with some data. I am having so much trouble trying to print/read what is inside the file. I tried the following: with gzip.open("myFil…

Pandas - combine row dates with column times

I have a dataframe:Date 0:15 0:30 0:45 ... 23:15 23:30 23:45 24:00 2004-05-01 3.74618 3.58507 3.30998 ... 2.97236 2.92008 2.80101 2.6067 2004-05-02 3.09098 3.846…

How to extract tables in Images

I wanted to extract tables from images.This python module https://pypi.org/project/ExtractTable/ with their website https://www.extracttable.com/pro.html doing the job very well but they have limited f…

Extract string if match the value in another list

I want to get the value of the lookup list instead of a boolean. I have tried the following codes:val = pd.DataFrame([An apple,a Banana,a cat,a dog]) lookup = [banana,dog] # I tried the follow code: va…

Automating HP Quality Center with Python or Java

We have a project that uses HP Quality Center and one of the regular issues we face is people not updating comments on the defect.So I was thinkingif we could come up with a small script or tool that c…