• <bdo id='3Ogr5'></bdo><ul id='3Ogr5'></ul>

    <legend id='3Ogr5'><style id='3Ogr5'><dir id='3Ogr5'><q id='3Ogr5'></q></dir></style></legend>

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

    2. <small id='3Ogr5'></small><noframes id='3Ogr5'>

        Python Pandas滚动聚合一列列表

        Python Pandas rolling aggregate a column of lists(Python Pandas滚动聚合一列列表)
      1. <tfoot id='WsiZP'></tfoot>

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

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

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

                • 本文介绍了Python Pandas滚动聚合一列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的dataframe df,其中有一列列表lists。我想根据lists生成一个附加列。

                  df如下所示:

                  import pandas as pd
                  lists={1:[[1]],2:[[1,2,3]],3:[[2,9,7,9]],4:[[2,7,3,5]]}
                  #create test dataframe
                  df=pd.DataFrame.from_dict(lists,orient='index')
                  df=df.rename(columns={0:'lists'})
                  df
                  
                            lists
                  1           [1]
                  2     [1, 2, 3]
                  3  [2, 9, 7, 9]
                  4  [2, 7, 3, 5]
                  

                  我希望df如下所示:

                  df
                  Out[9]: 
                            lists                 rolllists
                  1           [1]                       [1]
                  2     [1, 2, 3]              [1, 1, 2, 3]
                  3  [2, 9, 7, 9]     [1, 2, 3, 2, 9, 7, 9]
                  4  [2, 7, 3, 5]  [2, 9, 7, 9, 2, 7, 3, 5]
                  
                  基本上我想‘求和’/append滚动的2个列表。请注意,第1行,因为我只有1个列表1,所以滚动列表就是那个列表。但是在第2行,我有2个我想要追加的列表。然后第三行,加上df[2].listsdf[3].lists等,我以前做过类似的事情,参考这个:Pandas Dataframe, Column of lists, Create column of sets of cumulative lists, and record by record differences。
                  此外,如果我们可以获得上面的这一部分,那么我想在groupby中执行此操作(例如,下面的示例将是1组,因此,例如df中的df可能类似于groupby):

                    Group         lists                 rolllists
                  1     A           [1]                       [1]
                  2     A     [1, 2, 3]              [1, 1, 2, 3]
                  3     A  [2, 9, 7, 9]     [1, 2, 3, 2, 9, 7, 9]
                  4     A  [2, 7, 3, 5]  [2, 9, 7, 9, 2, 7, 3, 5]
                  5     B           [1]                       [1]
                  6     B     [1, 2, 3]              [1, 1, 2, 3]
                  7     B  [2, 9, 7, 9]     [1, 2, 3, 2, 9, 7, 9]
                  8     B  [2, 7, 3, 5]  [2, 9, 7, 9, 2, 7, 3, 5]
                  

                  我尝试了各种方法,如df.lists.Rolling(2).sum(),得到以下错误:

                  TypeError: cannot handle this type -> object 
                  

                  在Pandas 0.24.1中和在Pandas 0.22.0中不走运,该命令不会出错,而是返回与lists中完全相同的值。所以看起来新版本的 pandas 不能把名单加起来?那是次要问题。

                  谢谢您的帮助!玩得开心!

                  推荐答案

                  您可以从

                  开始
                  import pandas as pd
                  mylists={1:[[1]],2:[[1,2,3]],3:[[2,9,7,9]],4:[[2,7,3,5]]}
                  mydf=pd.DataFrame.from_dict(mylists,orient='index')
                  mydf=mydf.rename(columns={0:'lists'})
                  mydf = pd.concat([mydf, mydf], axis=0, ignore_index=True)
                  mydf['group'] = ['A']*4 + ['B']*4
                  
                  # initialize your new series
                  mydf['newseries'] = mydf['lists']
                  
                  # define the function that appends lists overs rows
                  def append_row_lists(data):
                      for i in data.index:
                          try: data.loc[i+1, 'newseries'] = data.loc[i, 'lists'] + data.loc[i+1, 'lists']
                          except: pass
                      return data
                  
                  # loop over your groups
                  for gp in mydf.group.unique():
                      condition = mydf.group == gp
                      mydf[condition] = append_row_lists(mydf[condition])
                  

                  输出

                            lists Group                 newseries
                  0           [1]     A                       [1]
                  1     [1, 2, 3]     A              [1, 1, 2, 3]
                  2  [2, 9, 7, 9]     A     [1, 2, 3, 2, 9, 7, 9]
                  3  [2, 7, 3, 5]     A  [2, 9, 7, 9, 2, 7, 3, 5]
                  4           [1]     B                       [1]
                  5     [1, 2, 3]     B              [1, 1, 2, 3]
                  6  [2, 9, 7, 9]     B     [1, 2, 3, 2, 9, 7, 9]
                  7  [2, 7, 3, 5]     B  [2, 9, 7, 9, 2, 7, 3, 5]
                  

                  这篇关于Python Pandas滚动聚合一列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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替换为非空值)

                    1. <tfoot id='53Eiu'></tfoot>
                        • <bdo id='53Eiu'></bdo><ul id='53Eiu'></ul>

                          <small id='53Eiu'></small><noframes id='53Eiu'>

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