简体   繁体   English

如何:Pandas 由于 pivot 表而存在多索引时的列划分

[英]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 resultSpend除以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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM