user3741679
user3741679

Reputation: 98

Pandas: How to display the largest difference for a column?

I am using (result['column1'] - result['column2']).abs().idxmax() to find the largest difference between two columns. But it only returns the 'index' to me without actually displaying the value.

For example:

Breeders    number of cats  number of dogs
a                  5               25
b                  15              15
c                  25              10

If I use (result['number of cats'] - result['number of dogs']).abs().idxmax() It would return 'a'.

What should I do if I wanna have a output like'a' with a difference of 20?

Upvotes: 1

Views: 128

Answers (2)

Joe
Joe

Reputation: 12417

Another option:

max_ix = (result['number of cats'] - result['number of dogs']).abs().idxmax()
max_br = result.loc[max_ix, "Breeders"]
max_diff = abs(result.loc[max_ix, 'number of cats'] - result.loc[max_ix, 'number of dogs'])
print  max_br + ' with a difference of ' + str(max_diff)

Upvotes: 1

jezrael
jezrael

Reputation: 863801

First create Series and then use format for print idxmax and max values:

s = (result['number of cats'] - result['number of dogs']).abs()
print (s)
a    20
b     0
c    15
dtype: int64

print ('{} with a difference of {}'.format(s.idxmax(), s.max()))
#thanks @jpp for python 3.6+ solution
#print(f'{s.idxmax()} with a difference of {s.max()}')
a with a difference of 20

Original solution:

a = (result['number of cats'] - result['number of dogs']).abs().agg(['idxmax','max'])
print (a)
idxmax     a
max       20
dtype: object

print ('{} with a difference of {}'.format(a['idxmax'], a['max']))
a with a difference of 20

Upvotes: 1

Related Questions