gpt4 book ai didi

python - Pandas 将行换行到下一列

转载 作者:行者123 更新时间:2023-12-01 09:33:16 25 4
gpt4 key购买 nike

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

最佳答案

您可以先创建MultiIndex,然后创建unstacksort_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com