首页 文章

将pandas数据帧拆分为N [关闭]的块

提问于
浏览
3

我目前正在尝试将一个pandas数据帧拆分为包含每N行的未知数量的块 .

我尝试过使用numpy.array_split()这个功能但是将数据帧拆分为N个包含未知行数的块 .

有没有一种聪明的方法可以将python数据帧拆分成多个数据帧,每个数据帧包含父数据帧中的特定行数

3 回答

  • 1

    您可以从N计算分割数:

    splits = int(np.floor(len(df.index)/N))
    chunks = np.split(df.iloc[:splits*N], splits)
    chunks.append(df.iloc[splits*N:])
    
  • 1

    你可以试试这个:

    def rolling(df, window, step):
        count = 0
        df_length = len(df)
        while count < (df_length -window):
            yield count, df[count:window+count]
            count += step
    

    用法:

    for offset, window in rolling(df, 100, 100):
        # |     |                      |     |
        # |     The current chunk.     |     How many rows to step at a time.
        # The current offset index.    How many rows in each chunk.
        # your code here
        pass
    

    还有一个更简单的想法:

    def chunk(seq, size):
        return (seq[pos:pos + size] for pos in range(0, len(seq), size))
    

    用法:

    for df_chunk in chunk(df, 100):
        #                     |
        #                     The chunk size
        # your code here
    

    BTW . 所有这些都可以在SO上找到,并进行搜索 .

  • 2

    计算分裂的索引:

    size_of_chunks =  3
    index_for_chunks = list(range(0, index.max(), size_of_chunks))
    index_for_chunks.extend([index.max()+1])
    

    用它们来拆分df:

    dfs = {}
    for i in range(len(index_for_chunks)-1):
        dfs[i] = df.iloc[index_for_chunks[i]:index_for_chunks[i+1]]
    

相关问题