<tfoot id='5UaeH'></tfoot>

        • <bdo id='5UaeH'></bdo><ul id='5UaeH'></ul>

        <small id='5UaeH'></small><noframes id='5UaeH'>

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

        在多个列上创建具有GroupBy的新滚动平均值列

        Create new rolling mean column with GroupBy on multiple columns(在多个列上创建具有GroupBy的新滚动平均值列)
          <bdo id='s2hXW'></bdo><ul id='s2hXW'></ul>

                <tbody id='s2hXW'></tbody>
              <legend id='s2hXW'><style id='s2hXW'><dir id='s2hXW'><q id='s2hXW'></q></dir></style></legend>

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

                <tfoot id='s2hXW'></tfoot>

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

                1. 本文介绍了在多个列上创建具有GroupBy的新滚动平均值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含11列的数据帧,其中date是一个索引。我正在尝试使用列total的滚动平均值创建一个新列。但是,我收到错误:TypeError:插入列的索引与框架索引不兼容

                  import pandas as pd
                  
                  df = pd.DataFrame({
                      'date':['2016-04-01','2016-05-01','2016-07-01','2016-08-01','2016-09-01',  '2019-04-01','2019-05-01','2019-06-01','2019-08-01','2019-09-01'],
                      'Country':['USA', 'USA', 'USA', 'USA', 'USA','USA', 'USA', 'USA', 'USA', 'USA'],
                      'Region':['Eastern','Eastern','Eastern','Eastern','Eastern','Eastern','Eastern','Eastern','Eastern','Eastern'],
                      'State':['New York','New York','New York','New York','New York','New York','New York','New York','New York','New York'],
                      'Supplier':['ABC','ABC','ABC','ABC','ABC','ABC','ABC','ABC','ABC','ABC'],
                      'Location':['Bin-1', 'Bin-1', 'Bin-1', 'Bin-1', 'Bin-1','Bin-1', 'Bin-1', 'Bin-1', 'Bin-1', 'Bin-1'],
                      'Year':[2016,2016,2016,2016,2016,2019,2019,2019,2019,2019],
                      'Month':[4,5,7,8,9,4,5,6,8,9],
                      'periodcode':[4,5,7,8,9,4,5,6,8,9],
                      'Product':['bike','bike','bike','bike','bike','bike','bike','bike','bike','bike'],
                      'total':[0,2000,1000,4000,0,2000,2000,1000,4000,600]})
                  df.set_index('date', inplace=True)
                  
                  df['mean'] = df.groupby(['Country','Region','State','Supplier','Location','Product'], as_index=False)['total'].rolling(3).mean().reset_index(level=0,drop=True)
                  df.head(10)
                  

                  但是,当我将year列包括到groupby(即

                  df['mean'] = df.groupby(['Country','Region','State','Supplier','Location','Product','Year'], as_index=False)['total'].rolling(3).mean().reset_index(level=0,drop=True) 
                  

                  我计算了滚动平均数。问题是,我希望分组排除Year

                  有什么想法吗?

                  推荐答案

                  根据我们在下面备注中的讨论,您希望计算每个组跨年度的滚动平均值,因此以下内容应该会为您提供所需的结果:

                  df['mean'] = df.groupby(['Country','Region','State','Supplier','Location','Product'])['total'].rolling(3).mean().reset_index().set_index("date")['total']
                  

                  关键是保留date索引(它允许您将计算的滚动平均值与原始数据帧中的一行进行匹配),并在total列提取滚动平均值计算返回的Series对象。

                  更多详细说明:

                  您的问题是没有Yeargroupby会导致DataFramedf不兼容,因此无法分配给df["mean"]

                  第一个变体提供Series交换机匹配索引:

                  df.groupby(['Country','Region','State','Supplier','Location','Product','Year'], as_index=False)['total'].rolling(3).mean().reset_index(level=0,drop=True)
                  
                  date
                  2016-04-01            NaN
                  2016-05-01            NaN
                  2016-07-01    1000.000000
                  2016-08-01    2333.333333
                  2016-09-01    1666.666667
                  2019-04-01            NaN
                  2019-05-01            NaN
                  2019-06-01    1666.666667
                  2019-08-01    2333.333333
                  2019-09-01    1866.666667
                  Name: total, dtype: float64
                  
                  但是,第二个变体(没有Year)产生DataFrame,其中date列中的每个条目都成为其自己的列。因此,您无法将其分配给df["mean"]

                  此问题的解决方案实际上取决于您试图解决的问题。但是,从概念上讲,如果您将date作为索引,则分配给df["mean"]Series中的每个date只能有一个值。

                  这篇关于在多个列上创建具有GroupBy的新滚动平均值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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替换为非空值)
                    <tbody id='uQ6dz'></tbody>

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

                          <legend id='uQ6dz'><style id='uQ6dz'><dir id='uQ6dz'><q id='uQ6dz'></q></dir></style></legend>
                            <bdo id='uQ6dz'></bdo><ul id='uQ6dz'></ul>

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