我的数据框太长,我想将其包装到下一列中。这种方法有效,但是我敢肯定有更好的方法。我想要一个适用于甚至更长的数据帧的答案,以模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)

最佳答案

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

关于python - Pandas 将行包装到下一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49770929/

10-13 00:25