Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python支持多索引选择值_Python 3.x_Pandas_Multi Index - Fatal编程技术网

Python 3.x Python支持多索引选择值

Python 3.x Python支持多索引选择值,python-3.x,pandas,multi-index,Python 3.x,Pandas,Multi Index,我有一个熊猫多重索引,大部分是数值,但数据中也有一些没有、NaN或“-”。大概是这样的: 0 1 2 3 bar one -0.096648 -0.080298 0.859359 -0.030288 two NaN -0.431791 1.923893 -1.544845 thr -0.358526 1.416211 1.589617 0.284130 baz one 0.63

我有一个熊猫多重索引,大部分是数值,但数据中也有一些没有、NaN或“-”。大概是这样的:

                0         1         2         3
bar one -0.096648 -0.080298  0.859359 -0.030288
    two       NaN -0.431791  1.923893 -1.544845
    thr -0.358526  1.416211  1.589617  0.284130
baz one  0.639951 -0.008833         -  0.042315
    two  0.705281      None -1.108522  0.471676
现在,我需要为每个级别0索引确定哪一行在第0列中具有最小的数值,并为该行提取第3列的值。(忽略NaN、None和-)

例如,对于'bar',我比较-0.096648,NaN,-0.358526,其中最小的是-0.358526,所以我想要值0.284130(来自第3列)

我相信这很简单,但我对这些多索引表不是很熟悉,只是迷路了,感到沮丧。

用于索引,但首先需要一些预处理,然后选择:

#get name for level of MultiIndex and create unique index
df1 = df.rename_axis(('a','b')).reset_index()
#if values non numeric in column 0 convert to NaNs
df1[0] = pd.to_numeric(df1[0], errors='coerce')
#get index of minimal values of column 0 per column a
s = df1.groupby('a')[0].idxmin()
print (s)
a
bar    2
baz    3
Name: 0, dtype: int64

#select by positions index and column 3
df = df.iloc[s, 3].to_frame()
print (df)
                3
bar thr  0.284130
baz one  0.042315