Python Pandas DataFrame 上的条件逻辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14714181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional Logic on Pandas DataFrame
提问by nitin
How to apply conditional logic to a Pandas DataFrame.
如何将条件逻辑应用于 Pandas DataFrame。
See DataFrame shown below,
请参阅如下所示的数据帧,
data desired_output
0 1 False
1 2 False
2 3 True
3 4 True
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.
我的原始数据显示在“数据”列中,并在其旁边显示了所需的输出。如果 'data' 中的数字低于 2.5,则所需输出为 False。
I could apply a loop and do re-construct the DataFrame... but that would be 'un-pythonic'
我可以应用一个循环并重新构建 DataFrame ......但这将是“非pythonic”
采纳答案by Jan Katins
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
回答by Zelazny7
In [1]: df
Out[1]:
data
0 1
1 2
2 3
3 4
You want to apply a function that conditionally returns a value based on the selected dataframe column.
您想应用一个函数,该函数根据选定的数据框列有条件地返回一个值。
In [2]: df['data'].apply(lambda x: 'true' if x <= 2.5 else 'false')
Out[2]:
0 true
1 true
2 false
3 false
Name: data
You can then assign that returned column to a new column in your dataframe:
然后,您可以将该返回的列分配给数据框中的新列:
In [3]: df['desired_output'] = df['data'].apply(lambda x: 'true' if x <= 2.5 else 'false')
In [4]: df
Out[4]:
data desired_output
0 1 true
1 2 true
2 3 false
3 4 false
回答by Andy Hayden
In this specific example, where the DataFrame is only one column, you can write this elegantly as:
在这个特定示例中,DataFrame 只有一列,您可以优雅地将其编写为:
df['desired_output'] = df.le(2.5)
letests whether elements are less than or equal 2.5, similarly ltfor less than, gtand ge.
le测试元素是否小于或等于 2.5,同样lt用于小于gt和ge。
回答by Surya
In [34]: import pandas as pd
In [35]: import numpy as np
In [36]: df = pd.DataFrame([1,2,3,4], columns=["data"])
In [37]: df
Out[37]:
data
0 1
1 2
2 3
3 4
In [38]: df["desired_output"] = np.where(df["data"] <2.5, "False", "True")
In [39]: df
Out[39]:
data desired_output
0 1 False
1 2 False
2 3 True
3 4 True

