Pandas 熊猫:如何显示列的最大差异?

Pandas 熊猫:如何显示列的最大差异?,pandas,Pandas,我正在使用(result['column1']-result['column2']).abs().idxmax()查找两列之间的最大差异。但它只向我返回“索引”,而没有实际显示值 例如: Breeders number of cats number of dogs a 5 25 b 15 15 c 25 10

我正在使用
(result['column1']-result['column2']).abs().idxmax()
查找两列之间的最大差异。但它只向我返回“索引”,而没有实际显示值

例如:

Breeders    number of cats  number of dogs
a                  5               25
b                  15              15
c                  25              10
如果我使用
(结果['number of cats']-result['number of dogs']).abs().idxmax()
它将返回
'a'


如果我想要像
'a'这样的输出值相差20
,我该怎么办?

首先创建
系列
,然后使用
格式
打印
idxmax
最大值
值:

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
原始解决方案:

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
另一种选择:

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)

可以保存扫描
s
两次并执行
s=(…).abs().agg(['idxmax',max'])
然后执行
print({}差{}。格式(s['idxmax',s['max'])
可能?@JonClements-是的,我的第一个解决方案。您还可以在3.6+中使用f字符串,
print(f'{s.idxmax差{s()}
,在我看来更具可读性:)@JonClements-Hmmm,只考虑混合
系列
-带字符串的数字,如果不以某种方式更改格式;)不知道你是什么意思?