Pandas将行包装到下一列

时间:2018-04-11 09:05:47

标签: python pandas

我的数据帧太长了,我想把它包装到下一列。这种方法有效,但我确信有更好的方法。我想要一个适用于更长数据帧的答案,以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 个答案:

答案 0 :(得分:1)

您可以首先创建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