Skip to main content
 首页 » 编程设计

python Pandas : Get row by median value

2025年05月04日10lhb25

我正在尝试获取列中值的行。

我正在使用 data.median() 获取“列”的中值。

id                 30444.5 
someProperty           3.0 
numberOfItems          0.0 
column                70.0 

data.median()['column'] 随后是:

data.median()['performance'] 
>>> 70.0 

如何获取中值的行或索引? 有没有类似idxmax/idxmin的东西?

我尝试过过滤,但在多行具有相同值的情况下它不可靠。

谢谢!

请您参考如下方法:

您可以使用 rank 和 idxmin 并将其应用于每一列:

import numpy as np 
import pandas as pd 
 
 
def get_median_index(d): 
    ranks = d.rank(pct=True) 
    close_to_median = abs(ranks - 0.5) 
    return close_to_median.idxmin() 
df = pd.DataFrame(np.random.randn(13, 4)) 
df 
    0           1           2           3 
0   0.919681    -0.934712   1.636177    -1.241359 
1   -1.198866   1.168437    1.044017    -2.487849 
2   1.159440    -1.764668   -0.470982   1.173863 
3   -0.055529   0.406662    0.272882    -0.318382 
4   -0.632588   0.451147    -0.181522   -0.145296 
5   1.180336    -0.768991   0.708926    -1.023846 
6   -0.059708   0.605231    1.102273    1.201167 
7   0.017064    -0.091870   0.256800    -0.219130 
8   -0.333725   -0.170327   -1.725664   -0.295963 
9   0.802023    0.163209    1.853383    -0.122511 
10  0.650980    -0.386218   -0.170424   1.569529 
11  0.678288    -0.006816   0.388679    -0.117963 
12  1.640222    1.608097    1.779814    1.028625 
df.apply(get_median_index, 0) 
0    7 
1    7 
2    3 
3    4