简体   繁体   English

基于布尔条件的 Pandas 数据框中的新列

[英]New column in Pandas dataframe based on boolean conditions

I'd like to create a new column to a Pandas dataframe populated with True or False based on the other values in each specific row.我想根据每个特定行中的其他值为填充了 True 或 False 的 Pandas 数据框创建一个新列。 My approach to solve this task was to apply a function checking boolean conditions across each row in the dataframe and populate the new column with either True or False.我解决此任务的方法是在数据框中的每一行中应用检查布尔条件的函数,并使用 True 或 False 填充新列。

This is the dataframe:这是数据框:

l={'DayTime':['2018-03-01','2018-03-02','2018-03-03'],'Pressure':
[9,10.5,10.5], 'Feed':[9,10.5,11], 'Temp':[9,10.5,11]}

df1=pd.DataFrame(l)

This is the function I wrote:这是我写的函数:

def ops_on(row):
   return row[('Feed' > 10)
              & ('Pressure' > 10)
              & ('Temp' > 10)
             ]

The function ops_on is used to create the new column ['ops_on']:函数 ops_on 用于创建新列 ['ops_on']:

df1['ops_on'] = df1.apply(ops_on, axis='columns')

Unfortunately, I get this error message:不幸的是,我收到此错误消息:

TypeError: ("'>' not supported between instances of 'str' and 'int'", 'occurred at index 0')类型错误:(“str”和“int”的实例之间不支持“'>'”,'发生在索引 0')

Thankful for help.感谢帮助。

You should work column-wise (vectorised, efficient) rather than row-wise (inefficient, Python loop):您应该按列(矢量化,高效)而不是按行(低效,Python 循环)工作:

df1['ops_on'] = (df1['Feed'] > 10) & (df1['Pressure'] > 10) & (df1['Temp'] > 10)

The & ("and") operator is applied to Boolean series element-wise. & (“and”)运算符应用于布尔系列元素。 An arbitrary number of such conditions can be chained.可以链接任意数量的此类条件。


Alternatively, for the special case where you are performing the same comparison multiple times:或者,对于您多次执行相同比较的特殊情况:

df1['ops_on'] = df1[['Feed', 'Pressure', 'Temp']].gt(10).all(1)

In your current setup, just re-write your function like this:在您当前的设置中,只需像这样重写您的函数:

def ops_on(row):
    return (row['Feed'] > 10) & (row['Pressure'] > 10) & (row['Temp'] > 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于来自熊猫中其他数据框的多个条件在数据框中创建新的布尔列 - How to create a new boolean column in a dataframe based on multiple conditions from other dataframe in pandas 根据熊猫数据框中的两个条件创建一列布尔值 - Making a column of boolean values based on two conditions in pandas dataframe 基于每个唯一值的条件的新pandas布尔列 - New pandas boolean column based on conditions for each unique value pandas dataframe 中的新列基于现有列值和条件列表 - New column in pandas dataframe based on existing column values with conditions list 根据 boolean 条件列表创建 pandas Dataframe - Create a pandas Dataframe based on on a list of boolean conditions Pandas DataFrame 根据多个条件分组添加新列值 - Pandas DataFrame add new column values based on group by multiple conditions 根据多个不同的条件在 pandas 数据框中创建了一个新列 - created a new column in a pandas dataframe based on multiple different conditions 根据不同条件在Pandas dataframe中新建一列 - Create a new column in Pandas dataframe based on different conditions 基于多个条件在 Pandas 数据框中创建一个新列 - Create a new column in pandas dataframe based on multiple conditions 如何根据此 Pandas 数据框中的列条件创建新的索引行? - How to create new index lines based on column conditions in this pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM