熊猫在数据框架中排序和bin数据以使其枢纽

发布于 2025-02-08 19:14:57 字数 573 浏览 2 评论 0原文

我有一个具有(随机)观察高度,周期和区域的数据框架:

Height = [1, 4, 3, 3, 3, 2, 4, 2, 3, 3, 3, 1, 4, 3, 3, 4, 1, 4, 2, 2]
Period = [5, 4, 2, 4, 2, 2, 3, 3, 5, 2, 4, 5, 4, 2, 4, 4, 3, 5, 4, 3]
Zone = [1,1,3,1,4,1,1,1,1,4,1,3,2,1,4,2,4,4,2,4]

Direction = [292.5,  22.5, 202.5, 337.5, 292.5, 337.5, 337.5, 337.5,  22.5, 292.5,  22.5, 157.5, 112.5, 337.5, 292.5, 112.5, 247.5, 247.5,
       112.5, 292.5]

我想在索引上用区域,列上的独特时期,然后对索引柱的每种组合做一个表格,我想拥有高度的最大值:

I have a dataframe with (random) observations for height, period and zones as such:

Height = [1, 4, 3, 3, 3, 2, 4, 2, 3, 3, 3, 1, 4, 3, 3, 4, 1, 4, 2, 2]
Period = [5, 4, 2, 4, 2, 2, 3, 3, 5, 2, 4, 5, 4, 2, 4, 4, 3, 5, 4, 3]
Zone = [1,1,3,1,4,1,1,1,1,4,1,3,2,1,4,2,4,4,2,4]

Direction = [292.5,  22.5, 202.5, 337.5, 292.5, 337.5, 337.5, 337.5,  22.5, 292.5,  22.5, 157.5, 112.5, 337.5, 292.5, 112.5, 247.5, 247.5,
       112.5, 292.5]

I want to make a table with the zones on the indices, the unique periods on the columns and then for each combination of index-column I want to have the maximum of the height as such:

enter image description here

Any idea how to do this?

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

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

发布评论

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

评论(1

も星光 2025-02-15 19:14:57

您可以尝试

out = (df.pivot_table(index='Zone', columns='Period', values='Height', aggfunc='max')
       .rename(index=lambda x: f'Zone={x}', columns=lambda x: f'Period={x}'))
print(out)

Period  Period=2  Period=3  Period=4  Period=5
Zone
Zone=1       3.0       4.0       4.0       3.0
Zone=2       NaN       NaN       4.0       NaN
Zone=3       3.0       NaN       NaN       1.0
Zone=4       3.0       2.0       3.0       4.0

You can try

out = (df.pivot_table(index='Zone', columns='Period', values='Height', aggfunc='max')
       .rename(index=lambda x: f'Zone={x}', columns=lambda x: f'Period={x}'))
print(out)

Period  Period=2  Period=3  Period=4  Period=5
Zone
Zone=1       3.0       4.0       4.0       3.0
Zone=2       NaN       NaN       4.0       NaN
Zone=3       3.0       NaN       NaN       1.0
Zone=4       3.0       2.0       3.0       4.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文