如何划分两个 Pandas 数据透视表以获得结果数据透视表
我有一个带有 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)
示例:
我想要第三组,它是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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论