从多索引数据框中提取行,其中儿童级别与numpy数组上的数字匹配
从以下多数索引数据框中:
a b c
0 0 0 42 65
1 6 0 340
2 5 71 800
3 2 51 409
4 0 23 279
5 8 38 549
1 0 1 23 252
1 9 13 977
2 1 19 943
2 0 2 23 295
1 3 39 458
2 1 62 308
3 0 95 954
4 9 78 535
3 0 4 67 849
1 3 46 761
2 7 49 485
3 0 44 638
如何从与numpy数组上的数字匹配的数据框架中提取行?例如,如果我的数组是:
a = np.array([2, 2, 4, 3])
结果应该是一个数据框架,例如:
a b c
0 5 71 800
1 1 19 943
2 9 78 535
3 0 44 638
我尝试了以下内容:
i,j = df.index.levels
ix = a
df1 = pd.DataFrame(df.to_numpy()[ix])
但这是给我错误的结果。我目前获得的数据框架是:
a b c
0 5 71 800
1 5 71 800
2 0 23 279
3 2 51 409
它实际上是在读取DF1而不是DF的索引。
From the following Multi-index dataframe:
a b c
0 0 0 42 65
1 6 0 340
2 5 71 800
3 2 51 409
4 0 23 279
5 8 38 549
1 0 1 23 252
1 9 13 977
2 1 19 943
2 0 2 23 295
1 3 39 458
2 1 62 308
3 0 95 954
4 9 78 535
3 0 4 67 849
1 3 46 761
2 7 49 485
3 0 44 638
How can I extract the rows from the dataframe that matches the numbers on a Numpy array? For instance, if my array is:
a = np.array([2, 2, 4, 3])
The result should be a dataframe like:
a b c
0 5 71 800
1 1 19 943
2 9 78 535
3 0 44 638
I have tried the following:
i,j = df.index.levels
ix = a
df1 = pd.DataFrame(df.to_numpy()[ix])
but that's giving me the wrong result. The dataframe I'm currently getting is:
a b c
0 5 71 800
1 5 71 800
2 0 23 279
3 2 51 409
It's actually reading the index from df1 instead of df.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要为每个级别使用一个数字,则可以使用直接切片:
输出:
如果要处理潜在的不正确数据,请使用
reindex
:输出:输出:
If you want to use one number for each level you could use direct slicing:
output:
If you want to handle potentially incorrect data, use
reindex
:output: