简体   繁体   English

如果使用 isin 和 bool() 满足条件,则将列添加到 dataframe

[英]Add a column to a dataframe if a condition is satisfied using isin and bool()

I have some problems with writing the right code to obtain the result that I want.我在编写正确的代码以获得我想要的结果时遇到了一些问题。 I have a df composed as follows:我有一个 df 组成如下:

df= ['Date','Error_Id','State','Alarm','Group','Production_type','Error_frequency']

There are 4 types of errors in the df (eg 1,2,3,4). df 中有 4 种类型的错误(例如 1、2、3、4)。

I would like to add a column of booleans for each error, with a 1 if the error is present in the row and a 0 if not.我想为每个错误添加一列布尔值,如果错误存在于行中,则为 1,否则为 0。

I have tried to do this with the if condition, the isin() method and the bool() method but as of now I obtained only errors.我尝试使用 if 条件、isin() 方法和 bool() 方法来做到这一点,但到目前为止我只得到了错误。 Hereafter the code I tried to use for the isin() and bool() methods此后我尝试用于 isin() 和 bool() 方法的代码

test = alarms[alarms.Error_Id.isin([1])].bool()

I also thought of creating a new df with this and the use it to for comparison with the original df but I think that this is not the right way to do it.我也想过用这个创建一个新的 df 并将其用于与原始 df 进行比较,但我认为这不是正确的方法。

Concerning the Error message with the isin() and bool() methods it is the following:关于 isin() 和 bool() 方法的错误消息如下:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM