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

    2. <tfoot id='pN2yg'></tfoot>
        <bdo id='pN2yg'></bdo><ul id='pN2yg'></ul>

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

      pandas :按移位分组和累加和(GroupBy Shift And Cumulative Sum)

      Pandas: GroupBy Shift And Cumulative Sum( pandas :按移位分组和累加和(GroupBy Shift And Cumulative Sum))
      <legend id='GNM4K'><style id='GNM4K'><dir id='GNM4K'><q id='GNM4K'></q></dir></style></legend>

          <bdo id='GNM4K'></bdo><ul id='GNM4K'></ul>

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

            1. <small id='GNM4K'></small><noframes id='GNM4K'>

              <tfoot id='GNM4K'></tfoot>

                本文介绍了 pandas :按移位分组和累加和(GroupBy Shift And Cumulative Sum)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想做groupbyshiftcumsum,这看起来相当琐碎的任务,但我仍然对得到的结果头晕目眩。有没有人能告诉我我做错了什么。我在网上找到的所有结果都显示出与我正在做的事情相同或相同的变化。下面是我的实现。

                temp = pd.DataFrame(data=[['a',1],['a',1],['a',1],['b',1],['b',1],['b',1],['c',1],['c',1]], columns=['ID','X'])
                
                temp['transformed'] = temp.groupby('ID')['X'].cumsum().shift()
                print(temp)
                
                   ID   X   transformed
                0   a   1   NaN
                1   a   1   1.0
                2   a   1   2.0
                3   b   1   3.0
                4   b   1   1.0
                5   b   1   2.0
                6   c   1   3.0
                7   c   1   1.0
                

                这是错误的,因为我要查找的实际内容或内容如下:

                   ID   X   transformed
                0   a   1   NaN
                1   a   1   1.0
                2   a   1   2.0
                3   b   1   NaN
                4   b   1   1.0
                5   b   1   2.0
                6   c   1   NaN
                7   c   1   1.0
                

                事先非常感谢。

                推荐答案

                您可以使用transform()将在groupby的每个级别创建的单独组馈送到cumsum()shift()方法中。

                temp['transformed'] = 
                    temp.groupby('ID')['X'].transform(lambda x: x.cumsum().shift())
                
                  ID  X   transformed
                0  a  1   NaN
                1  a  1   1.0
                2  a  1   2.0
                3  b  1   NaN
                4  b  1   1.0
                5  b  1   2.0
                6  c  1   NaN
                7  c  1   1.0
                

                有关transform()的更多信息,请查看此处:

                • https://jakevdp.github.io/PythonDataScienceHandbook/03.08-aggregation-and-grouping.html#Transformation
                • https://pandas.pydata.org/pandas-docs/version/0.22/groupby.html#transformation

                这篇关于 pandas :按移位分组和累加和(GroupBy Shift And Cumulative Sum)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='bNyJz'></bdo><ul id='bNyJz'></ul>
                  • <small id='bNyJz'></small><noframes id='bNyJz'>

                      <tbody id='bNyJz'></tbody>

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