python - 基于numpy数组从多索引数据框中获取数据

标签 python pandas multi-index

来自以下数据框:

dim_0 dim_1                                             
0     0       40.54  23.40  6.70  1.70  1.82  0.96  1.62
      1      175.89  20.24  7.78  1.55  1.45  0.80  1.44
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
1     0       21.38  24.00  5.90  1.60  2.55  1.50  2.36
      1      130.29  18.40  8.49  1.52  1.45  0.80  1.47
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
2     0        6.30  25.70  5.60  1.70  2.16  1.16  1.87    
      1       73.45  21.49  6.88  1.61  1.61  0.94  1.63
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
3     0       16.64  25.70  5.70  1.60  2.17  1.12  1.76
      1      125.89  19.10  7.52  1.43  1.44  0.78  1.40
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
4     0       41.38  24.70  5.60  1.50  2.08  1.16  1.85
      1        0.00   0.00  0.00  0.00  0.00  0.00  0.00
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
5     0      180.59  16.40  3.80  1.10  4.63  3.86  5.71
      1        0.00   0.00  0.00  0.00  0.00  0.00  0.00
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
6     0       13.59  24.40  6.10  1.70  2.62  1.51  2.36
      1      103.19  19.02  8.70  1.53  1.48  0.76  1.38
      2        0.00   0.00  0.00  0.00  0.00  0.00  0.00
7     0        3.15  24.70  5.60  1.50  2.14  1.22  2.00
      1       55.90  23.10  6.07  1.50  1.86  1.12  1.87
      2      208.04  20.39  6.82  1.35  1.47  0.95  1.67

如何仅获取 dim_01 中与数组 [1 0 0 1 2 0 1 2] 匹配的行?

期望的结果是:

 0      175.89  20.24  7.78  1.55  1.45  0.80  1.44
 1       21.38  24.00  5.90  1.60  2.55  1.50  2.36
 2        6.30  25.70  5.60  1.70  2.16  1.16  1.87
 3      125.89  19.10  7.52  1.43  1.44  0.78  1.40
 4        0.00   0.00  0.00  0.00  0.00  0.00  0.00
 5      180.59  16.40  3.80  1.10  4.63  3.86  5.71
 7      103.19  19.02  8.70  1.53  1.48  0.76  1.38
 8      208.04  20.39  6.82  1.35  1.47  0.95  1.67

我尝试过使用切片、横截面等,但没有成功。

预先感谢您的帮助。

最佳答案

使用MultiIndex.from_arrays并选择DataFrame.loc :

arr = np.array([1, 0, 0, 1, 2, 0, 1 ,2])

df = df.loc[pd.MultiIndex.from_arrays([df.index.levels[0], arr])]
print (df)
          2      3     4     5     6     7     8
0                                               
0 1  175.89  20.24  7.78  1.55  1.45  0.80  1.44
1 0   21.38  24.00  5.90  1.60  2.55  1.50  2.36
2 0    6.30  25.70  5.60  1.70  2.16  1.16  1.87
3 1  125.89  19.10  7.52  1.43  1.44  0.78  1.40
4 2    0.00   0.00  0.00  0.00  0.00  0.00  0.00
5 0  180.59  16.40  3.80  1.10  4.63  3.86  5.71
6 1  103.19  19.02  8.70  1.53  1.48  0.76  1.38
7 2  208.04  20.39  6.82  1.35  1.47  0.95  1.67

arr = np.array([1, 0, 0, 1, 2, 0, 1 ,2])
df = df.loc[pd.MultiIndex.from_arrays([df.index.levels[0], arr])].droplevel(1)
print (df)
        2      3     4     5     6     7     8
0                                             
0  175.89  20.24  7.78  1.55  1.45  0.80  1.44
1   21.38  24.00  5.90  1.60  2.55  1.50  2.36
2    6.30  25.70  5.60  1.70  2.16  1.16  1.87
3  125.89  19.10  7.52  1.43  1.44  0.78  1.40
4    0.00   0.00  0.00  0.00  0.00  0.00  0.00
5  180.59  16.40  3.80  1.10  4.63  3.86  5.71
6  103.19  19.02  8.70  1.53  1.48  0.76  1.38
7  208.04  20.39  6.82  1.35  1.47  0.95  1.67

关于python - 基于numpy数组从多索引数据框中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63825319/

相关文章:

python - 如何摆脱 Pandas 中的多维索引

python - 为什么 ... == True 在 Python 3 中返回 False?

python - 值错误 : `Concatenate` layer requires inputs with matching shapes except for the concat axis

python - 使用 pandas df 旋转 sns 热图

python - 将聚合函数应用于 Pandas 分层索引中的多个索引片的系统方法

python - 通过切片索引和条件行设置值

python - 通过 python 扩展/包装器传递 float 组指针 – SndObj-library

python - 计算轨迹(路径)中的转折点/枢轴点

python - 如何填写 Pandas 每组的最后一行?

python - 我怎样才能预测我的变量