Home > Blockchain >  Numpy where: unsupported operand type(s) for |: 'int' and 'float'
Numpy where: unsupported operand type(s) for |: 'int' and 'float'

Time:08-27

In dataframe df1 I'm trying to set the value of cell [0,1] equal to:

--> 1 if the previous cell of same row is equal to 0 OR if a dot product of first rows of df2 and df3 is below 100

--> 0 in all other cases

I tried the following code:

df1.iloc[0, 1] = np.where(df1.iloc[0, 0] == 0  |  df2.iloc[0, :] .dot (df3.iloc[0, :])) <=  100   , 1, 0)

but I get the following error: TypeError: unsupported operand type(s) for |: 'int' and 'float'

I also tried to change the datatype of the first column of df1 to float, but without success What am I doing wrong? Thanks

CodePudding user response:

You're missing parentheses around the separate parts of the or-expression.

Your original code is also incorrect, since there is an extra closing parenthesis.

Below, I've written what I think is the expression you want:

df1.iloc[0, 1] = np.where(
          (df1.iloc[0, 0] == 0)  |  
          (df2.iloc[0, :].dot(df3.iloc[0, :]) <=  100),
       1, 0)
  • Related