user4599
user4599

Reputation: 127

adding boolean column in pandas dataframe

with this code i want to create boolean column check to check if the value in the column LTP is in the range of 0.10% of column val.

all values are of type float.

   df = df.assign(upper_range=lambda x: (x['val'] +(x['val'] * 0.001)))
   df = df.assign(lower_range=lambda x: ( x['val']- (x['val'] * 0.001) ) )
   df = df.assign(check=lambda x: (x['LTP'] >= x['lower_range'] & x['LTP'] <= x['upper_range']))

this code gave error

    TypeError: ufunc 'bitwise_and' not supported for the input types, 
    and the inputs could not be safely coerced to any supported types according to the casting rule 
    ''safe''

    TypeError: unsupported operand type(s) for &: 'float' and 'float'

for comparison i also tried using and instead of & for which following error occured

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

how to fix this?

Upvotes: 0

Views: 239

Answers (1)

jezrael
jezrael

Reputation: 862511

There is not closed () correctly, need parantheses for each condition:

 df = df.assign(check=lambda x: (x['LTP'] >= x['lower_range']) & (x['LTP'] <= x['upper_range']))

Upvotes: 1

Related Questions