沿多索引和独立索引切片的 pandas [英] Pandas slicing along multiindex and separate indices

查看:66
本文介绍了沿多索引和独立索引切片的 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始将Pandas用于一些大型数据集,并且大多数情况下都运行良好.我对索引有一些疑问

I've started using Pandas for some large Datasets and mostly it works really well. There are some questions I have regarding the indices though

  1. 我有一个具有三个级别的MultiIndex-假设a,b,c.我如何沿索引a切片-我只想要a = 5、7、10、13的值.按照文档中的说明进行df.ix [[5、7、10、13]]无效

  1. I have a MultiIndex with three levels - let's say a, b, c. How do I slice along index a - I just want the values where a = 5, 7, 10, 13. Doing df.ix[[5, 7, 10, 13]] does not work as pointed out in the documentation

我需要在DF上具有不同的索引-我可以创建这些多个索引而不将它们与数据框相关联,而不能使用它们将原始的ndarray索引还给我吗?

I need to have different indices on a DF - can I create these multiple indices and not associate them to a dataframe and use them to give me back the raw ndarray index?

我可以不在序列或数据框中单独分割MultiIndex吗?

Can I slice a MultiIndex on its own not in a series or Dataframe?

预先感谢

推荐答案

对于第一部分,您可以使用

For the first part, you can use boolean indexing using get_level_values:

df[df.index.get_level_values('a').isin([5, 7, 10, 13])]

对于后两个,您可以检查 MultiIndex 对象,方法是调用:

For the second two, you can inspect the MultiIndex object by calling:

df.index

(可以检查/切片.)

这篇关于沿多索引和独立索引切片的 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆