从多索引数据框中提取行,其中儿童级别与numpy数组上的数字匹配

发布于 2025-02-12 02:31:46 字数 916 浏览 2 评论 0原文

从以下多数索引数据框中:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-02-19 02:31:46

如果要为每个级别使用一个数字,则可以使用直接切片:

a = np.array([2, 2, 4, 3])
b = df.index.get_level_values(0).unique().to_numpy() # to_numpy() is optional
# b = array([0, 1, 2, 3])

df.loc[zip(b,a)]

输出:

     a   b    c
0 2  5  71  800
1 2  1  19  943
2 4  9  78  535
3 3  0  44  638

如果要处理潜在的不正确数据,请使用 reindex

a = np.array([2, 5, 0, 2])
# b = df.index.get_level_values(0).unique()

df.reindex(zip(b,a))

输出:输出:

       a     b      c
0 2  5.0  71.0  800.0
1 5  NaN   NaN    NaN
2 0  2.0  23.0  295.0
3 2  7.0  49.0  485.0

If you want to use one number for each level you could use direct slicing:

a = np.array([2, 2, 4, 3])
b = df.index.get_level_values(0).unique().to_numpy() # to_numpy() is optional
# b = array([0, 1, 2, 3])

df.loc[zip(b,a)]

output:

     a   b    c
0 2  5  71  800
1 2  1  19  943
2 4  9  78  535
3 3  0  44  638

If you want to handle potentially incorrect data, use reindex:

a = np.array([2, 5, 0, 2])
# b = df.index.get_level_values(0).unique()

df.reindex(zip(b,a))

output:

       a     b      c
0 2  5.0  71.0  800.0
1 5  NaN   NaN    NaN
2 0  2.0  23.0  295.0
3 2  7.0  49.0  485.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文