[英]pandas create a boolean column in df
Here's some made data:这是一些制作的数据:
import pandas as pd
import numpy as np
import operator
rows,cols = 8760,3
data = np.random.rand(rows,cols)
tidx = pd.date_range('2019-01-01', periods=rows, freq='1T')
df = pd.DataFrame(data, columns=['Mix_Temp','Outside_Temp','Return_Temp'], index=tidx)
How can I create another pandas column that is Boolean True or False based on a fault condition that I incorporated into a function:如何根据我合并到函数中的故障条件创建另一个为布尔值 True 或 False 的 Pandas 列:
def fault_condition_two_(dataframe):
return operator.truth(dataframe.Mix_Temp + dataframe.mat_err < min((dataframe.Return_Temp - dataframe.rat_err) , (dataframe.Outside_Temp - dataframe.oat_err)))
There are some additional params
below if I try this:如果我尝试这个,下面还有一些额外的params
:
# params
rat_err = 2.
mat_err = 5.
oat_err = 5.
# make columns out of params
df['rat_err'] = rat_err
df['mat_err'] = mat_err
df['oat_err'] = oat_err
# run data thru function
df['bool_flag'] = fault_condition_two_(df)
Ill get this famous ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我会得到这个著名的ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
which I cant seem to find out a better solution.我似乎无法找到更好的解决方案。 Any tips greatly appreciated.任何提示非常感谢。
Note that I did have luck using the Python operator with this function below on a different fault condition equation where this worked great.请注意,我确实很幸运地在不同的故障条件方程上使用了 Python运算符和下面的这个函数,这很好用。 What's best practice?什么是最佳实践? This function works fine, no errors:此功能工作正常,没有错误:
def fault_condition_one(dataframe):
return operator.and_(dataframe.duct_static < dataframe.duct_static_setpoint, dataframe.vfd_speed >= dataframe.vfd_speed_percent_max - dataframe.vfd_speed_percent_err_thres)
IIUC use numpy version for min
by numpy.minimum.
IIUC 通过numpy.minimum.
为min
使用 numpy 版本numpy.minimum.
, here operator.truth
is not necessary: ,这里operator.truth
不是必需的:
def fault_condition_two_(dataframe):
return (dataframe.Mix_Temp + dataframe.mat_err < np.minimum((dataframe.Return_Temp - dataframe.rat_err) , (dataframe.Outside_Temp - dataframe.oat_err)))
An alternative using only direct dataframe manipulation:仅使用直接数据帧操作的替代方法:
((df.Mix_Temp + df.mat_err) < (df.Return_Temp - df.rat_err))\
& ((df.Mix_Temp + df.mat_err) < (df.Return_Temp - df.rat_err))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.