Python Pandas按二级索引(或任何其他级别)切片multiindex [英] Python Pandas slice multiindex by second level index (or any other level)
问题描述
有很多帖子将多索引的级别[0]按级别范围进行切片 1 级别[0]索引值的索引
There are many postings on slicing the level[0] of a multiindex by a range of level1. However, I cannot find a solution for my problem; that is, I need a range of the level1 index for level[0] index values
数据帧:第一个是A到Z,等级是1到400;我需要每个level [0]的前2个和后2个(第一个),但不需要在同一步骤中.
dataframe: First is A to Z, Rank is 1 to 400; I need the first 2 and last 2 for each level[0] (First), but not in the same step.
Title Score
First Rank
A 1 foo 100
2 bar 90
3 lime 80
4 lame 70
B 1 foo 400
2 lime 300
3 lame 200
4 dime 100
我正在尝试获取每个级别的最后2行 1 带有以下代码的索引,但仅对第一个level [0]值正确地切片.
I am trying to get the last 2 rows for each level1 index with the below code, but it slices properly only for the first level[0] value.
[IN] df.ix[x.index.levels[1][-2]:]
[OUT]
Title Score
First Rank
A 3 lime 80
4 lame 70
B 1 foo 400
2 lime 300
3 lame 200
4 dime 100
我通过交换索引获得了前2行,但是我无法使其适用于后2行.
The first 2 rows I get by swapping the indices, but I cannot make it work for the last 2 rows.
df.index = df.index.swaplevel("Rank", "First")
df= df.sortlevel() #to sort by Rank
df.ix[1:2] #Produces the first 2 ranks with 2 level[1] (First) each.
Title Score
Rank First
1 A foo 100
B foo 400
2 A bar 90
B lime 300
我当然可以换回去得到这个:
Of course I can swap this back to get this:
df2 = df.ix[1:2]
df2.index = ttt.index.swaplevel("First","rank") #change the order of the indices back.
df2.sortlevel()
Title Score
First Rank
A 1 foo 100
2 bar 90
B 1 foo 400
2 lime 300
希望通过相同的步骤获得任何帮助:
Any help is appreciated to get with the same procedure:
- 最后2行用于索引 1 (排名)
- 获得前两行的更好方法
- Last 2 rows for index1 (Rank)
- And a better way to get the first 2 rows
通过@ako编辑以下反馈:
Edit following feedback by @ako:
真正使用pd.IndexSlice
可以很容易地切片任何级别的索引.这里是一个更通用的解决方案,下面是我逐步采用的方法来获取第一行和最后两行.此处的更多信息: http://pandas.pydata.org/pandas -docs/stable/advanced.html#using-slicers
Using pd.IndexSlice
truly makes it easy to slice any level index. Here a more generic solution and below my step-wise approach to get the first and last two rows. More information here: http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers
"""
Slicing a dataframe at the level[2] index of the
major axis (row) for specific and at the level[1] index for columns.
"""
df.loc[idx[:,:,['some label','another label']],idx[:,'yet another label']]
"""
Thanks to @ako below is my solution, including how I
get the top and last 2 rows.
"""
idx = pd.IndexSlice
# Top 2
df.loc[idx[:,[1,2],:] #[1,2] is NOT a row index, it is the rank label.
# Last 2
max = len(df.index.levels[df.index.names.index("rank")]) # unique rank labels
last2=[x for x in range(max-2,max)]
df.loc[idx[:,last2],:] #for last 2 - assuming all level[0] have the same lengths.
推荐答案
使用索引器将任意值切成任意维度-只需传递带有该维度所需级别/值的列表即可.
Use an indexer to slice arbitrary values in arbitrary dimensions--just pass a list with whatever the desired levels / values are for that dimension.
idx = pd.IndexSlice
df.loc[idx[:,[3,4]],:]
Title Score
First Rank
A 3 lime 80
4 lame 70
B 3 lame 200
4 dime 100
用于再现数据:
from StringIO import StringIO
s="""
First Rank Title Score
A 1 foo 100
A 2 bar 90
A 3 lime 80
A 4 lame 70
B 1 foo 400
B 2 lime 300
B 3 lame 200
B 4 dime 100
"""
df = pd.read_csv(StringIO(s),
sep='\s+',
index_col=["First", "Rank"])
这篇关于Python Pandas按二级索引(或任何其他级别)切片multiindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!