Python 在 Pandas 中将 .loc 与 MultiIndex 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24435788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:36:17  来源:igfitidea点击:

Using .loc with a MultiIndex in pandas?

pythonpandasdataframemulti-index

提问by kronosapiens

Does anyone know if it is possible to use the DataFrame.loc method to select from a MultiIndex? I have the following DataFrame and would like to be able to access the values located in the 'Dwell' columns, at the indices of ('at', 1), ('at', 3), ('at', 5), and so on (non-sequential).

有谁知道是否可以使用 DataFrame.loc 方法从 MultiIndex 中进行选择?我有以下的数据帧,并希望能够访问位于“暂停”列中的值,在指数('at', 1)('at', 3)('at', 5),等(非连续)。

I'd love to be able to do something like data.loc[['at',[1,3,5]], 'Dwell'], similar to the data.loc[[1,3,5], 'Dwell']syntax for a regular index (which returns a 3-member series of Dwell values).

我很想能够做data.loc[['at',[1,3,5]], 'Dwell']类似的事情,类似于data.loc[[1,3,5], 'Dwell']常规索引的语法(它返回一个 3 成员系列的 Dwell 值)。

My purpose is to select an arbitrary subset of the data, perform some analysis only on that subset, and then update the new values with the results of the analysis. I plan on using the same syntax to set new values for these data, so chaining selectors wouldn't really work in this case.

我的目的是选择数据的任意子集,仅对该子集执行一些分析,然后使用分析结果更新新值。我计划使用相同的语法为这些数据设置新值,因此在这种情况下链接选择器不会真正起作用。

Here is a slice of the DataFrame I'm working with:

这是我正在使用的 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  

Thanks!

谢谢!

采纳答案by chrisb

If you are on version 0.14, you can simply pass a tuple to .locas below:

如果您使用的是 0.14 版本,则可以简单地将元组传递给.loc如下:

df.loc[('at', [1,3,4]), 'Dwell']

回答by R. Max

Try the cross-sectionindexing:

尝试横截面索引:

In [68]: df.xs('at', level='QGram', drop_level=False).loc[[1,4]]
Out[68]: 
        Char  Dwell  Flight  ND_Offset  Offset
QGram                                         
at    1    t    180       0   0.108363       5
      4    a     20     180   0.000000       0