代码之家  ›  专栏  ›  技术社区  ›  Hatshepsut

熊猫将行包装到下一列中

  •  1
  • Hatshepsut  · 技术社区  · 7 年前

    我的数据帧太长,我想将其包装到下一列中。这种方法可行,但我相信还有更好的方法。我想要一个能够处理更长数据帧的答案,将其封装在第1行的模3上。

    import pandas as pd
    import numpy as np
    
    
    
    def wraparound(df, row_number):
        """row_number is the first number that we wrap onto the next column."""
        n = row_number - 1
        r = df.iloc[:n]
        r = pd.concat([r, df.iloc[n:2*n].reset_index(drop=True)], axis=1)
        r = pd.concat([r, df.iloc[2 * n:3*n].reset_index(drop=True)], axis=1)
        r = r.reset_index(drop=True).T.reset_index(drop=True).T
        return r
    
    df = pd.DataFrame.from_records([
        (1, 11),
        (2, 12),
        (3, 13),
        (4, 14),
        (5, 15),
        (6, 16),
        (7, 17),
    ])
    
    result = wraparound(df, 4)
    
    expected = pd.DataFrame.from_records([
        (1, 11, 4, 14, 7, 17),
        (2, 12, 5, 15, np.nan, np.nan),
        (3, 13, 6, 16, np.nan, np.nan),
    ])
    
    
    pd.testing.assert_frame_equal(result, expected)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   jezrael    7 年前

    您可以创建 MultiIndex 首先,然后 unstack 具有 sort_index :

    N = 3
    a = np.arange(len(df))
    df.index = [a % N, a // N]
    df = df.unstack().sort_index(axis=1, level=1)
    df.columns = np.arange(len(df.columns))
    print (df)
         0     1    2     3    4     5
    0  1.0  11.0  4.0  14.0  7.0  17.0
    1  2.0  12.0  5.0  15.0  NaN   NaN
    2  3.0  13.0  6.0  16.0  NaN   NaN