[英]How to: Pandas division of columns when multiindex present due to pivot table
I have a sales dataset which I read into a Pandas dataframe and pivoted to group information.我有一个销售数据集,我将其读入 Pandas dataframe 并转向组信息。 Now columns are a Multiindex and looks like this:
现在列是一个多索引,看起来像这样:
MultiIndex([('Orders', 'Pants'),
('Orders', 'Shirts'),
('Orders', 'Shoes'),
( 'Spend', 'Pants'),
( 'Spend', 'Shirts'),
( 'Spend', 'Shoes'),,
names=[None, 'Product'])
Now, I am trying to calculate an "average order value" for each "product".现在,我正在尝试计算每个“产品”的“平均订单价值”。 I want to do something like:
我想做类似的事情:
# Calculate Avg. Order Value by dividing Spend Series by Orders Series
dataframe['AOV'] = dataframe['Spend'] / dataframe['Orders']
But I cannot.但是我不能。 How do I end up with a dataframe that has these column multiindex:
我如何以具有这些列多索引的 dataframe 结束:
MultiIndex([('Orders', 'Pants'),
('Orders', 'Shirts'),
('Orders', 'Shoes'),
( 'Spend', 'Pants'),
( 'Spend', 'Shirts'),
( 'Spend', 'Shoes'),
( 'AOV', 'Pants'),
( 'AOV', 'Shirts'),
( 'AOV', 'Shoes'),
names=[None, 'Product'])
Divide Spend
by Orders
then concat
with keys
parameter to append additional index level called AOV
then concat
with the original dataframe to get the result将
Spend
除以Orders
,然后与keys
参数连接到concat
称为AOV
的附加索引级别,然后与原始concat
连接以获得结果
AOV = pd.concat([df.loc['Spend'] / df.loc['Orders']], keys=['AOV'])
result = pd.concat([df, AOV])
Alternatively you can unstack
the dataframe then divide and stack back或者,您可以拆开
unstack
然后分开并堆叠回去
s = df.unstack()
s.loc['AOV'] = s.loc['Spend'] / s.loc['Orders']
result = s.stack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.