I do not understand why this works
df[(df['Gold']>0) & (df['Gold.1']>0)].loc[((df['Gold'] - df['Gold.1'])/(df['Gold'])).abs().idxmax()]
but when I divide by (df['Gold'] + df['Gold.1'] + df['Gold.2'])
it stops working giving me error that you can find below.
Interestingly, the following line works
df.loc[((df['Gold'] - df['Gold.1'])/(df['Gold'] + df['Gold.1'] + df['Gold.2'])).abs().idxmax()]
I do not understand what is happening since I just started to learn Python and Pandas. I need to understand the reason why this happens and how to fix it.
ERROR
KeyError: 'the label [Algeria] is not in the [index]'

Your problem is
boolean indexing:returns a filtered DataFrame which does not contain the
indexofmaxvalue ofSeriesyou calculated with this:In your data it is
Algeria.So
loclogically throws aKeyError.One possible solution is to assign the new filtered
DataFrametodf1and then get the index corresponding to the max value ofSeriesby usingidxmax: