Find the row that has the maximum difference between the two columns

I have a DataFrame with Goldand columns Gold.1. I want to find a row where the difference of these two columns is maximal.

For the next DataFrame, this should return me a row of 6.

df
Out: 
   Gold  Gold.1
0     2       1
1     1       4
2     6       9
3     4       4
4     4       8
5     5       5
6     5       2 ---> The difference is maximum (3)
7     5       9
8     5       3
9     5       6

I tried using the following:

df.where(max(df['Gold']-df['Gold.1']))

However, this caused a ValueError:

df.where (max (df ['Gold'] - df ['Gold.1']))
Traceback (most recent call last):

  File "", line 1, in 
    df.where (max (df ['Gold'] - df ['Gold.1']))

  File "../python3.5/site-packages/pandas/core/generic.py", line 5195, in where
    raise_on_error)

  File "../python3.5/site-packages/pandas/core/generic.py", line 4936, in _where
    raise ValueError ('Array conditional must be same shape as'

ValueError: Array conditional must be same shape as self

, ?

+4
1

.where .idxmax:

(df['Gold'] - df['Gold.1']).idxmax()
Out: 6

, .

, .abs().

(df['Gold'] - df['Gold.1']).abs().idxmax()
Out: 4
+11

Source: https://habr.com/ru/post/1662738/


All Articles