Python pandas在多个列上聚合groupby,然后是pi

2024-04-18 02:13:43 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,我有一个类似于以下内容的pandas数据框:

Item | shop1 | shop2 | shop3 | Category
------------------------------------
Shoes| 45    | 50    | 53    | Clothes
TV   | 200   | 300   | 250   | Technology
Book | 20    | 17    | 21    | Books
phone| 300   | 350   | 400   | Technology

其中shop1、shop2和shop3是不同商店中每个商品的成本。 现在,我需要返回一个数据帧,在一些数据清理之后,像下面这样:

Category (index)| size| sum| mean | std
----------------------------------------

其中,尺寸是每类商品的数量和总和,平均值和标准值与适用于3个商店的相同功能相关。如何使用split apply combine模式(groupby、aggregate、apply…)执行这些操作?

有人能帮我吗?我要疯了…谢谢!


Tags: 数据pandastvitem商品商店applycategory
3条回答

为Pandas 0.22+编辑,考虑到通过聚合在一个组中使用词典的不足。

我们建立了一个非常相似的字典,我们使用字典的键来指定我们的函数,并使用字典本身来重命名列。

rnm_cols = dict(size='Size', sum='Sum', mean='Mean', std='Std')
df.set_index(['Category', 'Item']).stack().groupby('Category') \
  .agg(rnm_cols.keys()).rename(columns=rnm_cols)

            Size   Sum        Mean        Std
Category                                     
Books          3    58   19.333333   2.081666
Clothes        3   148   49.333333   4.041452
Technology     6  1800  300.000000  70.710678

选项1
使用^{}☆链接到文档

agg_funcs = dict(Size='size', Sum='sum', Mean='mean', Std='std')
df.set_index(['Category', 'Item']).stack().groupby(level=0).agg(agg_funcs)

                  Std   Sum        Mean  Size
Category                                     
Books        2.081666    58   19.333333     3
Clothes      4.041452   148   49.333333     3
Technology  70.710678  1800  300.000000     6

选项2
多多少少
使用^{}☆链接到文档

df.set_index(['Category', 'Item']).stack().groupby(level=0).describe().unstack()

            count        mean        std    min    25%    50%    75%    max
Category                                                                   
Books         3.0   19.333333   2.081666   17.0   18.5   20.0   20.5   21.0
Clothes       3.0   49.333333   4.041452   45.0   47.5   50.0   51.5   53.0
Technology    6.0  300.000000  70.710678  200.0  262.5  300.0  337.5  400.0
df.groupby('Category').agg({'Item':'size','shop1':['sum','mean','std'],'shop2':['sum','mean','std'],'shop3':['sum','mean','std']})

或者,如果你想在所有商店都买,那么:

df1 = df.set_index(['Item','Category']).stack().reset_index().rename(columns={'level_2':'Shops',0:'costs'})
df1.groupby('Category').agg({'Item':'size','costs':['sum','mean','std']})

如果我理解正确,您需要计算所有商店的汇总指标,而不是每个商店的汇总指标。为此,您可以首先^{}数据帧,然后按Category分组:

stacked = df.set_index(['Item', 'Category']).stack().reset_index()
stacked.columns = ['Item', 'Category', 'Shop', 'Price']
stacked.groupby('Category').agg({'Price':['count','sum','mean','std']})

结果是

           Price                             
           count   sum        mean        std
Category                                     
Books          3    58   19.333333   2.081666
Clothes        3   148   49.333333   4.041452
Technology     6  1800  300.000000  70.710678

相关问题 更多 >