'The truth value of a Series is ambiguous' – pandas

Question:

I’m trying to compare stock price data between two columns in pandas but keep getting this error.

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

for n in df['labels']:
    if df['Open'] > df['Close']:
          df['labels'] = 'green'
    else:
        df['labels'] = 'red'

The issue is this line – ‘f df[‘Open’] > df[‘Close’]’

Tried using .gt() method but that didn’t work – any suggestions?

Answers:

You are comparing two entire columns with each other which is ambiguos.

What you want to do instead is compare the values for each row in your dataframe.

Example df:

df = pd.DataFrame(columns=['open', 'close'], data=[[100, 105], [200, 195], [300, 301], [400, 404], [500, 499]])

You can use for example .itertuples() like that:

for row in df.itertuples():
    print(row.close > row.open)

The beauty of dataframes is however, that you can calculate multiple values at the same time and usually dont want to loop over your data.

df['labels'] = df.apply(lambda row: 'green' if row["close"] >= row["open"] else 'red', axis=1)

Here I create a new çolumn ‘labels’ and set the value to ‘green’ or ‘red’ depending on the values of open and close

The final df looks like this:

   open  close labels
0   100    105  green
1   200    195    red
2   300    301  green
3   400    404  green
4   500    499    red
Answered By: bitflip
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.