在 pandas 中使用带有多重索引的.loc [英] Using .loc with a MultiIndex in pandas
本文介绍了在 pandas 中使用带有多重索引的.loc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataFrame.loc
方法从MultiIndex
中进行选择?我有以下DataFrame
,并且希望能够访问位于('at', 1)
、('at', 3)
、('at', 5)
等索引(非顺序)的Dwell
列中的值。
我希望能够执行类似data.loc[['at',[1,3,5]], 'Dwell']
的操作,类似于常规索引的data.loc[[1,3,5], 'Dwell']
语法(它返回由三个成员组成的Dwell
值序列)。
以下是我正在使用的DataFrame
的片段:
Char Dwell Flight ND_Offset Offset
QGram
at 0 a 100 120 0.000000 0
1 t 180 0 0.108363 5
2 a 100 120 0.000000 0
3 t 180 0 0.108363 5
4 a 20 180 0.000000 0
5 t 80 120 0.108363 5
6 a 20 180 0.000000 0
7 t 80 120 0.108363 5
8 a 20 180 0.000000 0
9 t 80 120 0.108363 5
10 a 120 180 0.000000 0
推荐答案
如果您使用的是0.14版本,则只需向.loc
传递一个元组即可,如下所示:
df.loc[('at', [1,3,4]), 'Dwell']
这篇关于在 pandas 中使用带有多重索引的.loc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文