【问题标题】:Split data-frames into multiple, with overlap rows将数据帧拆分为多个,具有重叠行
【发布时间】:2019-09-21 16:52:05
【问题描述】:

需要将其拆分为多个数据帧的数据帧。每 6 行(自上而下)成为一个新的数据框。

下面的行可以正常工作,如屏幕截图。

import pandas as pd

data = {'ID': ["a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19","a20","a21","a22"], 
'Unit_Weight': [178,153,193,195,214,157,205,212,219,166,217,186,170,207,204,201,179,215,213,170,217,199]}

df = pd.DataFrame(data)

size = 6      # 6 rows in a new data-frame
list_of_dfs = [df.loc[i:i+size-1,:] for i in range(0, len(df),size)]

for l_d in list_of_dfs:
    print l_d

现在我想从下往上做,因为 df_2,它包括前一个数据帧的最后 2 行。

在 Python 中实现它的正确方法是什么?谢谢。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    这对我有用:-

    import pandas as pd
    
    data = {'ID': ["a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19","a20","a21","a22"], 
    'Unit_Weight': [178,153,193,195,214,157,205,212,219,166,217,186,170,207,204,201,179,215,213,170,217,199]}
    
    df = pd.DataFrame(data)
    
    
    size = 6      # 6 rows in a new data-frame
    list_of_dfs = [df.loc[i:i+size-1,:] for i in range(0, len(df),size-2) if i <len(df)-2]
    
    for l_d in list_of_dfs:
        print (l_d)
    

    【讨论】:

      【解决方案2】:

      我会向前工作,但只是向后计算数据帧数。

      df_num = 5
      li_dicts = {}
      
      for x in range(0,17,4):
          y = x + 6
          df_temp = df.iloc[x:y,:]
          li_dicts[("df_" + str(df_num))] = df_temp
          df_num -= 1
      
      li_dicts['df_5']
      
         ID  Unit_Weight
      0  a1          178
      1  a2          153
      2  a3          193
      3  a4          195
      4  a5          214
      5  a6          157
      

      【讨论】:

      • 我更喜欢@Udit Hari Vashisht 的回答!
      猜你喜欢
      • 2011-08-04
      • 2016-02-28
      • 2013-11-16
      相关资源
      最近更新 更多