Search code examples
python-3.xpandasstring-comparison

If/else in pandas ValueError


I have the following code:

if data['Open'] >= data['Close']:
    data['Color'] = 'True'

What i want to do is compare the two addGreen or Red in [Color] column in pandas. But i am getting

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

How can i fix this?


Solution

  • Try this to see if it works:

    import numpy as np
    data['Color'] = np.where(data['Open'] >= data['Close'], 'True', 'whatever_value_you_want_if_the_condition_is_false')