pandas DataFrame的条件逻辑

编程入门 行业动态 更新时间:2024-10-28 20:22:22
本文介绍了 pandas DataFrame的条件逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将条件逻辑应用于Pandas DataFrame.

How to apply conditional logic to a Pandas DataFrame.

请参见下面显示的DataFrame,

See DataFrame shown below,

data desired_output 0 1 False 1 2 False 2 3 True 3 4 True

我的原始数据显示在数据"列中,并且期望的输出显示在其旁边.如果数据"中的数字小于2.5,则所需的输出为False.

My original data is show in the 'data' column and the desired_output is shown next to it. If the number in 'data' is below 2.5, the desired_output is False.

我可以应用一个循环并重新构建DataFrame ...但是那是非pythonic的"

I could apply a loop and do re-construct the DataFrame... but that would be 'un-pythonic'

推荐答案

只需将列与该值进行比较:

Just compare the column with that value:

In [9]: df = pandas.DataFrame([1,2,3,4], columns=["data"]) In [10]: df Out[10]: data 0 1 1 2 2 3 3 4 In [11]: df["desired"] = df["data"] > 2.5 In [11]: df Out[12]: data desired 0 1 False 1 2 False 2 3 True 3 4 True

更多推荐

pandas DataFrame的条件逻辑

本文发布于:2023-10-25 03:28:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逻辑   条件   pandas   DataFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!