chr*_*868 7 python dataframe pandas
我想知道是否有办法找到数据框中最高值的位置(列和行索引).所以,例如,如果我的数据框看起来像这样:
A B C D E
0 100 9 1 12 6
1 80 10 67 15 91
2 20 67 1 56 23
3 12 51 5 10 58
4 73 28 72 25 1
Run Code Online (Sandbox Code Playgroud)
如何获得如下结果:[0, 'A']使用Pandas?
Mik*_*ler 12
np.argmaxNumPy argmax可以提供帮助:
>>> df.stack().index[np.argmax(df.values)]
(0, 'A')
Run Code Online (Sandbox Code Playgroud)
df.values 是一个二维NumPy数组:
>>> df.values
array([[100, 9, 1, 12, 6],
[ 80, 10, 67, 15, 91],
[ 20, 67, 1, 56, 23],
[ 12, 51, 5, 10, 58],
[ 73, 28, 72, 25, 1]])
Run Code Online (Sandbox Code Playgroud)
argmax 为您提供"flattened"数组最大值的索引:
>>> np.argmax(df.values)
0
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用此索引查找堆叠数据框上的行列位置:
>>> df.stack().index[0]
(0, 'A')
Run Code Online (Sandbox Code Playgroud)
如果您需要快速,请尽可能少地执行.仅在NumPy数组上工作以查找索引np.argmax似乎是最好的:
v = df.values
i, j = [x[0] for x in np.unravel_index([np.argmax(v)], v.shape)]
[df.index[i], df.columns[j]]
Run Code Online (Sandbox Code Playgroud)
结果:
[0, 'A']
Run Code Online (Sandbox Code Playgroud)
时序最适合lareg数据帧:
df = pd.DataFrame(data=np.arange(int(1e6)).reshape(-1,5), columns=list('ABCDE'))
Run Code Online (Sandbox Code Playgroud)
排序最慢到最快:
%timeit df.mask(~(df==df.max().max())).stack().index.tolist()
33.4 ms ± 982 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Run Code Online (Sandbox Code Playgroud)
%timeit list(df.stack().idxmax())
17.1 ms ± 139 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Run Code Online (Sandbox Code Playgroud)
%timeit df.stack().index[np.argmax(df.values)]
14.8 ms ± 392 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Run Code Online (Sandbox Code Playgroud)
%%timeit
i,j = np.where(df.values == df.values.max())
list((df.index[i].values.tolist()[0],df.columns[j].values.tolist()[0]))
4.45 ms ± 84.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Run Code Online (Sandbox Code Playgroud)
%%timeit
v = df.values
i, j = [x[0] for x in np.unravel_index([np.argmax(v)], v.shape)]
[df.index[i], df.columns[j]]
499 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Run Code Online (Sandbox Code Playgroud)
d = {'name': ['Mask', 'Stack-idmax', 'Stack-argmax', 'Where', 'Argmax-unravel_index'],
'time': [33.4, 17.1, 14.8, 4.45, 499],
'unit': ['ms', 'ms', 'ms', 'ms', 'µs']}
timings = pd.DataFrame(d)
timings['seconds'] = timings.time * timings.unit.map({'ms': 1e-3, 'µs': 1e-6})
timings['factor slower'] = timings.seconds / timings.seconds.min()
timings.sort_values('factor slower')
Run Code Online (Sandbox Code Playgroud)
输出:
name time unit seconds factor slower
4 Argmax-unravel_index 499.00 µs 0.000499 1.000000
3 Where 4.45 ms 0.004450 8.917836
2 Stack-argmax 14.80 ms 0.014800 29.659319
1 Stack-idmax 17.10 ms 0.017100 34.268537
0 Mask 33.40 ms 0.033400 66.933868
Run Code Online (Sandbox Code Playgroud)
因此,对于大型数据帧,"Argmax-unravel_index"版本似乎要快一到两个数量级,即通常速度最重要的地方.
使用stack了Series与MultiIndex和idxmax为最大值的指标:
print (df.stack().idxmax())
(0, 'A')
print (list(df.stack().idxmax()))
[0, 'A']
Run Code Online (Sandbox Code Playgroud)
详情:
print (df.stack())
0 A 100
B 9
C 1
D 12
E 6
1 A 80
B 10
C 67
D 15
E 91
2 A 20
B 67
C 1
D 56
E 23
3 A 12
B 51
C 5
D 10
E 58
4 A 73
B 28
C 72
D 25
E 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)