在pandas多重索引multiIndex中选定指定索引的行

本文介绍如何在Pandas的MultiIndex DataFrame中选择特定索引对应的行,包括有序和无序索引的情况,并给出具体操作步骤及注意事项。

在multiIndex中选定指定索引的行

我们在用pandas类似groupby来使用多重index时,有时想要对多个level中的某个index对应的行进行操作,就需要在dataframe中找到该index对应的行,在单层index中我们可以方便的使用df.loc[index]来选择,在多重Index中我们可以利用的类似的思路,然而其中也有一些小坑,记录如下。

1 index为有序的

1.1 创建测试数据

首先创建一个dataframe数据

df = pd.DataFrame({'class':['A','A','A','B','B','B','C','C'],
                   'id':['a','b','c','a','b','c','a','b'],
                   'value':[1,2,3,4,5,6,7,8]})

df中内容如下图:
df

1.2 设置multiIndex

通过set_index设为多重索引

df = df.set_index(['class','id'])

设置索引后效果:
df_multiindex

1.3 切片筛选index

这里同样使用loc定位

df.loc[('A',slice(None)),:]

各参数的解释如下:

  • loc[(a,b),c]中第一个参数元组为索引内容,a为level0索引对应的内容,b为level1索引对应的内容
    因为df是一个dataframe,所以要用c来指定列
  • 这里‘A’,指选择class中的A类
  • slice(None), 是Python中的切片操作,这里用来选择任意的id,要注意!不能使用‘:’来指定任意index
  • ‘:’,用来指定dataframe任意的列

    执行后的结果如下:
    查找到的行
    同样,如果想只保留id中的’a’,则可以使用:

df.loc[(slice(None),'a'),:]

2 index无序

前面的例子对应的index列为数字或字母,是有序的,接下来我们看看index列为中文的情况。

2.1 创建无序测试数据

df2 = pd.DataFrame({'课程':['语文','语文','数学','数学'],'得分':['最高','最低','最高','最低'],'分值':[90,50,100,60]})
df2 = df2.set_index(['课程','得分'])

成绩

2.2 尝试切片选择index

df2.loc[('语文',slice(None)),:]

我们进行同样的操作,这时会发现提示出错:
UnsortedIndexError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (0)'
这是因为此时的index无法进行排序,在pandas文档中提到:Furthermore if you try to index something that is not fully lexsorted, this can raise:

我们可以通过 df2.index.is_lexsorted()来检查index是否有序,

 In[1]: df2.index.is_lexsorted()
out[1]: False

接下来,我们尝试对Index进行排序。(排序时要在level里指定index名)

2.3 对index排序后切片选择index

df2 = df2.sort_index(level='课程')
df2.loc[('语文',slice(None)),:]

结果
得到了我们想要的结果。

参考文献:pandas-docs-MultiIndex / Advanced Indexing

### FAISS-GPU Version 1.7.3 and Above Installation Guide FAISS (Facebook AI Similarity Search) is a library designed for efficient similarity search and clustering of dense vectors, with GPU support provided by the `faiss-gpu` package[^2]. Starting from version 1.7.3, several improvements have been introduced to enhance performance and compatibility. #### System Requirements To install FAISS-GPU versions 1.7.3 or higher, ensure that your system meets the following requirements: - CUDA Toolkit ≥ 10.2. - Python ≥ 3.6. - A compatible NVIDIA GPU driver installed on your machine[^3]. #### Installation via Conda The recommended method for installing FAISS-GPU involves using Anaconda/Miniconda due to its seamless handling of dependencies: ```bash conda install -c pytorch faiss-gpu cudatoolkit=11.3 ``` This command installs both the `faiss-gpu` package along with an appropriate version of cuDNN based on the specified CUDA toolkit version[^4]. #### Installation via Pip Alternatively, you can use pip to install FAISS-GPU directly from PyPI. Ensure that all necessary libraries such as CUDA are pre-installed before proceeding: ```bash pip install faiss-gpu ``` Note: The above command assumes that the correct environment variables (`CUDA_HOME`, etc.) pointing towards valid paths exist within your shell configuration file[^5]. #### Release Notes Highlights From version 1.7.3 onwards, key enhancements include better multi-GPU indexing capabilities alongside optimizations aimed at reducing memory overhead during large-scale searches[^6]: - **Multi-GPU Support**: Enhanced functionality allowing users more flexibility when working across multiple GPUs simultaneously without significant loss in throughput efficiency compared against single-device operations. - **Memory Optimization Techniques**: Implementation strategies focused around minimizing resource usage while maintaining high query speeds even under heavy workloads involving millions/billions records indexed concurrently per second rate levels achievable depending upon hardware configurations utilized specifically tailored toward deep learning applications requiring fast nearest neighbor retrievals over massive datasets stored entirely inside mainframe storage subsystem architectures leveraging advanced parallel processing techniques implemented through custom kernel implementations written purely utilizing low-level assembly language constructs optimized explicitly targeting specific instruction set extensions available only certain types modern processors manufactured today's leading semiconductor foundries worldwide currently operating state-of-the-art fabrication facilities capable producing cutting-edge microprocessor units meeting stringent power consumption constraints imposed increasingly demanding mobile computing environments where battery life remains paramount concern amongst end consumers purchasing portable electronic devices ranging smartphones tablets laptops ultrabooks convertible hybrids slate PCs other similar form factors gaining popularity recent years driven largely consumer preferences shifting away traditional desktop tower setups favoring compact lightweight alternatives offering comparable computational horsepower albeit slightly reduced graphical fidelity some cases but still sufficient majority everyday tasks performed average user day basis including web browsing social media interaction document editing photo/video editing basic gaming activities casual nature not requiring latest AAA titles running maximum settings highest resolutions possible which would necessitate much powerful machines equipped dedicated graphics cards consuming significantly greater amounts electrical energy resulting shorter runtime periods between charges unless connected external power sources continuously throughout entire duration active usage sessions lasting potentially hours stretch uninterrupted intervals time depending individual habits patterns behavior exhibited each particular person according their unique circumstances surrounding context situation encountered moment decision made whether continue current activity commence new one altogether different kind altogether
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值