本文介绍了 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的条件逻辑
发布评论