1

I have some problems with writing the right code to obtain the result that I want. I have a df composed as follows:

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

There are 4 types of errors in the df (e.g. 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.

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. Hereafter the code I tried to use for the isin() and bool() methods

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.

Concerning the Error message with the isin() and bool() methods it is the following:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ALollz
  • 57,915
  • 7
  • 66
  • 89
sroverto
  • 57
  • 7
  • look at the [melt](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html) method – Will Sep 18 '19 at 21:10
  • 3
    You would want `alarms.Error_Id.eq(1).astype(int)`. Notice you are trying to change the column to an integer with `astype`, not call `.bool`. But if you just have 4 errors, you are probably looking for `pd.get_dummies(alarms.Error_Id)` which you can concatenate back to the original. – ALollz Sep 18 '19 at 21:16
  • For instance, see: https://stackoverflow.com/questions/23208745/adding-dummy-columns-to-the-original-dataframe – ALollz Sep 18 '19 at 21:23

0 Answers0