大熊猫长到宽阔的窗口

时间:2015-04-23 10:10:46

标签: python pandas

我正在尝试通过将行数据的“窗口”转换为列数据来重塑数据帧。例如,窗口大小为2,给定数据框:

    A   B
 0  a1  b1
 1  a2  b2
 2  a3  b3
 3  a4  b4

我想生成数据框:

    A1 A2 B1 B2
 0  a1 a2 b1 b2
 1  a2 a3 b2 b3
 2  a3 a4 b3 b4

这很棘手,因为旧数据框中的单元格在生成的数据框中可能没有唯一索引。

我当然可以做一些复杂的事情,例如迭代旧数据框中的行,计算新单元格中的单元格位置,以及简单地复制数据。但我想要一个更简单的解决方案......

1 个答案:

答案 0 :(得分:0)

您可以查看窗口大小为2的操作,将DataFrame向上移动一行,将其与原始DataFrame水平连接,最后进行一些重新排序。因此,无需迭代行,就可以这样做:

res = df.merge(df.shift(-1), left_index=True, right_index=True).iloc[:-1]
res.columns = ['A1', 'B1', 'A2', 'B2']
res = res[['A1', 'A2', 'B1', 'B2']]
print res

输出:

   A1  A2  B1  B2
0  a1  a2  b1  b2
1  a2  a3  b2  b3
2  a3  a4  b3  b4

这可以推广到任意DataFrame和窗口大小:

def rolling(df, window_size=2):
    dfs = [df]
    for i in range(1, window_size):
        dfs.append(df.shift(-i))
    res = pd.concat(dfs, axis=1).iloc[:-(window_size-1)]
    colnames = [c + str(i) for i in range(1, window_size+1) for c in df.columns]
    reorder = [c + str(i) for c in df.columns for i in range(1, window_size+1)]
    res.columns = colnames
    return res[reorder]

print rolling(df, 3)

输出:

   A1  A2  A3  B1  B2  B3
0  a1  a2  a3  b1  b2  b3
1  a2  a3  a4  b2  b3  b4
相关问题