我有一个带有列Gold和的数据框架Gold.1.我想找到这两列的差异最大的行.
对于以下DataFrame,这应该返回第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
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下内容:
df.where(max(df['Gold']-df['Gold.1']))
Run Code Online (Sandbox Code Playgroud)
但是这引发了一个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
如何找到满足此条件的行?
ayh*_*han 21
而不是.where,您可以使用.idxmax:
(df['Gold'] - df['Gold.1']).idxmax()
Out: 6
Run Code Online (Sandbox Code Playgroud)
这将返回差异最大的索引.
如果要查找具有最大绝对差异的行,则可以.abs()先调用.
(df['Gold'] - df['Gold.1']).abs().idxmax()
Out: 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9576 次 |
| 最近记录: |