如何通过仅指定n-th Index级别来从PANDAS DataFrame中选择行?

发布于 2025-01-29 11:17:33 字数 627 浏览 5 评论 0原文

我有一个带有多级行索引的熊猫数据框:

                 operators  license
                       sum      sum
City      Year
-----------------------------------       
New York  2020          44       A2
Chicago   2020          30       A2
Boston    2020          33       A1
New York  2021          48       A2
Chicago   2021          30       A2 
Boston    2021          41       A1

我可以通过仅指定行索引的级别0:

df.loc [(“ boston”,) ]

但是如何选择所有行,其中1级别为2020而不指定级别0? 搜索后,我发现查询给我确切的结果:

df.query(“ Year == 2020”)

现在我想知道如何要使用.loc实现这一目标?

I have a pandas DataFrame with a multi-level row index:

                 operators  license
                       sum      sum
City      Year
-----------------------------------       
New York  2020          44       A2
Chicago   2020          30       A2
Boston    2020          33       A1
New York  2021          48       A2
Chicago   2021          30       A2 
Boston    2021          41       A1

I can select rows with .loc by specifying only level 0 of the row index:

df.loc[("Boston", )]

But how do I select all rows where level 1 is 2020 without specifying level 0?
After some searching I found that query gives me the exact result I'm looking for:

df.query("Year == 2020")

Now I would like to know how to achieve this using .loc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

染柒℉ 2025-02-05 11:17:33

根据 cross section | MultiIndex /高级索引您可以使用

out = df.loc[(slice(None), 2020), :]

idx = pd.IndexSlice
out = df.loc[idx[:, 2020], :]

out = df.xs(2020, level='Year', drop_level=False)
out = df.xs(2020, level=1, drop_level=False)
print(out)

              operators license
                    sum     sum
City     Year
New York 2020        44      A2
Chicago  2020        30      A2
Boston   2020        33      A1

According to the Cross-section | MultiIndex / advanced indexing, you can use

out = df.loc[(slice(None), 2020), :]

idx = pd.IndexSlice
out = df.loc[idx[:, 2020], :]

out = df.xs(2020, level='Year', drop_level=False)
out = df.xs(2020, level=1, drop_level=False)
print(out)

              operators license
                    sum     sum
City     Year
New York 2020        44      A2
Chicago  2020        30      A2
Boston   2020        33      A1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文