如何划分两个 Pandas 数据透视表以获得结果数据透视表

发布于 2025-01-10 10:36:45 字数 1134 浏览 3 评论 0原文

我有一个带有 2 个值的 Pandas 数据透视表:已取消订单和总订单(月份:1=1 月、2 月 2 日、11=11 月、12=12 月和国家/地区)。

df_Cancellations_group_GEN1 = df_Cancellations_GEN1.pivot_table(index='country', columns='month', values=['total_orders', 'cancelled_due_to_partner'], aggfunc=sum, fill_value=0)

示例:

Table

我想要第三组,它是cancelled_due_to_partner/total_orders(相同结构月份/国家)的划分。它可以聚合到这个数据帧或一个新的 df,只需要代码来获取除法。

类似这样的

预期表

我尝试将 df 和潜水分开,但没有用。

df_Cancellations_group_GEN1_cancel = df_Cancellations_GEN1.pivot_table(index='country', columns='month', values=['cancelled_due_to_partner'], aggfunc=sum, fill_value=0)

df_Cancellations_group_GEN1_orders = df_Cancellations_GEN1.pivot_table(index='country', columns='month', value=['total_orders''], aggfunc=sum, fill_value=0)

--> df_Cancellations_group_GEN1_cancel.div(df_Cancellations_group_GEN1_orders)

--> 不起作用

I have one Pandas pivoted tables with 2 values: cancelled and total orders (with months: 1=January, 2 February, 11=November, 12=December and countries).

df_Cancellations_group_GEN1 = df_Cancellations_GEN1.pivot_table(index='country', columns='month', values=['total_orders', 'cancelled_due_to_partner'], aggfunc=sum, fill_value=0)

Example:

Table

I want to have a third group which is the division of cancelled_due_to_partner/total_orders (same structure month/country). It could be aggregated to this dataframe or a new df, just need the code to get the division.

Something like this:

Expected Table

I tried separating the df and diving, but dind't work.

df_Cancellations_group_GEN1_cancel = df_Cancellations_GEN1.pivot_table(index='country', columns='month', values=['cancelled_due_to_partner'], aggfunc=sum, fill_value=0)

df_Cancellations_group_GEN1_orders = df_Cancellations_GEN1.pivot_table(index='country', columns='month', values=['total_orders''], aggfunc=sum, fill_value=0)

--> df_Cancellations_group_GEN1_cancel.div(df_Cancellations_group_GEN1_orders)

--> Dind't work.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文