简体   繁体   中英

Python Pandas combine multiple boolean series

I have a dataframe with multiple columns such as below

gender marital education
male single tertiary

I have a list of requirements that contain boolean series and I need to combine them all using & sign.

bool_gender = df["gender"] == "male"
bool_marital = df["marital"] == "married"
bool_education = df["education"] == "secondary"
[bool_gender, bool_marital, bool_education]

How can I combine all of the items in the list using functional programming in Python 3 to obtain a single boolean value that is the result of the following expression:

desired output = bool_gender & bool_marital & bool_education


Possible use: 

reduce("&", map(function, iter))


您可以使用numpy.bitwise_andreduce版本:

np.bitwise_and.reduce([bool_gender, bool_marital, bool_education])

you could do something like this with the operator module and functools.reduce function:

>>> import operator
>>> from functools import reduce
>>> lst = [True, True, False]
>>> reduce(operator.and_,lst)
False 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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