Python Pandas结合了多个布尔系列

发布于 2025-02-09 13:31:23 字数 789 浏览 3 评论 0 原文

我有一个带有多个列的数据框,例如下面的

性别 婚姻 教育
男性 单一 三级

我有一系列包含布尔式系列的要求,我需要使用&符号。

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

如何使用Python 3中的功能编程来组合列表中的所有项目,以获得以下表达式的单个布尔值:

desired output = bool_gender & bool_marital & bool_education


Possible use: 

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


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))


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2025-02-16 13:31:23

您可以使用 操作员 模块和功能:

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

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 
荒芜了季节 2025-02-16 13:31:23

您可以使用 降低a> numpy> numpy.bit.bit.and.bit_and

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

You can use the reduce version of numpy.bitwise_and:

np.bitwise_and.reduce([bool_gender, bool_marital, bool_education])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文