• <tfoot id='6icHU'></tfoot>
      • <bdo id='6icHU'></bdo><ul id='6icHU'></ul>

        <i id='6icHU'><tr id='6icHU'><dt id='6icHU'><q id='6icHU'><span id='6icHU'><b id='6icHU'><form id='6icHU'><ins id='6icHU'></ins><ul id='6icHU'></ul><sub id='6icHU'></sub></form><legend id='6icHU'></legend><bdo id='6icHU'><pre id='6icHU'><center id='6icHU'></center></pre></bdo></b><th id='6icHU'></th></span></q></dt></tr></i><div id='6icHU'><tfoot id='6icHU'></tfoot><dl id='6icHU'><fieldset id='6icHU'></fieldset></dl></div>
        <legend id='6icHU'><style id='6icHU'><dir id='6icHU'><q id='6icHU'></q></dir></style></legend>
      1. <small id='6icHU'></small><noframes id='6icHU'>

        将组平均值分配给Python/PANAS中的每一行

        Assign group averages to each row in python/pandas(将组平均值分配给Python/PANAS中的每一行)

                <bdo id='ERByA'></bdo><ul id='ERByA'></ul>
              • <i id='ERByA'><tr id='ERByA'><dt id='ERByA'><q id='ERByA'><span id='ERByA'><b id='ERByA'><form id='ERByA'><ins id='ERByA'></ins><ul id='ERByA'></ul><sub id='ERByA'></sub></form><legend id='ERByA'></legend><bdo id='ERByA'><pre id='ERByA'><center id='ERByA'></center></pre></bdo></b><th id='ERByA'></th></span></q></dt></tr></i><div id='ERByA'><tfoot id='ERByA'></tfoot><dl id='ERByA'><fieldset id='ERByA'></fieldset></dl></div>

                <small id='ERByA'></small><noframes id='ERByA'>

              • <tfoot id='ERByA'></tfoot>
                    <tbody id='ERByA'></tbody>
                • <legend id='ERByA'><style id='ERByA'><dir id='ERByA'><q id='ERByA'></q></dir></style></legend>
                  本文介绍了将组平均值分配给Python/PANAS中的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据帧,我希望根据商店和所有商店计算平均值。我创建了计算平均值的代码,但我正在寻找一种更有效的方法。

                  DF

                  Cashier#     Store#     Sales    Refunds
                  001          001        100      1
                  002          001        150      2
                  003          001        200      2
                  004          002        400      1
                  005          002        600      4
                  

                  DF-所需

                  Cashier#     Store#     Sales    Refunds     Sales_StoreAvg    Sales_All_Stores_Avg
                  001          001        100      1            150               290
                  002          001        150      2            150               290
                  003          001        200      2            150               290
                  004          002        400      1            500               290
                  005          002        600      4            500               290
                  

                  我的尝试 我创建了另外两个数据帧,然后执行左连接

                  df.groupby(['Store#']).sum().reset_index().groupby('Sales').mean() 
                  

                  推荐答案

                  我认为新列需要GroupBy.transformmean的聚合值填充:

                  df['Sales_StoreAvg'] = df.groupby('Store#')['Sales'].transform('mean')
                  df['Sales_All_Stores_Avg'] = df['Sales'].mean()
                  print (df)
                     Cashier#  Store#  Sales  Refunds  Sales_StoreAvg  Sales_All_Stores_Avg
                  0         1       1    100        1             150                 290.0
                  1         2       1    150        2             150                 290.0
                  2         3       1    200        2             150                 290.0
                  3         4       2    400        1             500                 290.0
                  4         5       2    600        4             500                 290.0
                  

                  这篇关于将组平均值分配给Python/PANAS中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                      <bdo id='H7jYE'></bdo><ul id='H7jYE'></ul>

                      • <tfoot id='H7jYE'></tfoot>

                            <legend id='H7jYE'><style id='H7jYE'><dir id='H7jYE'><q id='H7jYE'></q></dir></style></legend>
                              <tbody id='H7jYE'></tbody>
                          1. <small id='H7jYE'></small><noframes id='H7jYE'>

                            <i id='H7jYE'><tr id='H7jYE'><dt id='H7jYE'><q id='H7jYE'><span id='H7jYE'><b id='H7jYE'><form id='H7jYE'><ins id='H7jYE'></ins><ul id='H7jYE'></ul><sub id='H7jYE'></sub></form><legend id='H7jYE'></legend><bdo id='H7jYE'><pre id='H7jYE'><center id='H7jYE'></center></pre></bdo></b><th id='H7jYE'></th></span></q></dt></tr></i><div id='H7jYE'><tfoot id='H7jYE'></tfoot><dl id='H7jYE'><fieldset id='H7jYE'></fieldset></dl></div>