Home > Enterprise >  Compare 2 values in Pandas Dataframe
Compare 2 values in Pandas Dataframe

Time:10-23

Hi I have this dataframe

enter image description here

and I am trying to compare which candy among 'Almond Joy' and '3 Musketeers' has the highest winpercent(last column).

musketeers = candy_data.loc[candy_data.competitorname == '3 Musketeers']['winpercent']
almond = candy_data.loc[candy_data.competitorname == 'Almond Joy']['winpercent']

print(musketeers)
print("===========================")
print(almond)

if musketeers > almond:
    more_popular = musketeers
else:
    more_popular = almond

But I got this error. Can someone tell me what is the problem here?

Thank you.

enter image description here

CodePudding user response:

You should get scalars, not series.

Try:

musketeers = candy_data.loc[candy_data.competitorname == '3 Musketeers']['winpercent'].mean()
almond = candy_data.loc[candy_data.competitorname == 'Almond Joy']['winpercent'].mean()
  • Related