Huy Pham
Huy Pham

Reputation: 71

Looking to obtain the biggest difference between two values in a column and returning the winner?

TeamA  TeamB   City
12      18      12
17      15       5
19       8       7

df['diff'] = numpy.abs(data_frame['TeamA'] - data_frame['TeamB'])
max = data_frame['difference'].max()

I am stuck at finding the difference max. Now, what I am looking to do is to get the winning team which should be team A in this case and then get the city number associated with that win. Any suggestion is appreciated.

Upvotes: 0

Views: 27

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Try:

diff = df['TeamA'] - df['TeamB']

max_row = diff.abs().idxmax()

team = 'Team A' if  diff.loc[max_row] > 0  else 'Team B'

city = df.loc[diff.abs().idxmax(), 'City']

Upvotes: 2

Related Questions