我有一个如下所示的pandas数据框:
id, value
1, a
2, b
3, c
.
.
1000, fsdf
我想将其转换为以下内容
id1, value1, id2, value2, ...... id5, value5
1, a, 2, b, 5, e
6
.
.
995, . .........................1000, fsdf
有没有办法用熊猫做这件事?
答案 0 :(得分:2)
您可以numpy.reshape
使用shape
:
print df
id value
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
5 6 f
6 7 g
7 8 h
8 9 i
9 10 j
print df.shape
(10, 2)
print df.values.reshape(df.shape[0] / 5, 10)
[[1L 'a' 2L 'b' 3L 'c' 4L 'd' 5L 'e']
[6L 'f' 7L 'g' 8L 'h' 9L 'i' 10L 'j']]
#see http://stackoverflow.com/a/35511057/2901002
cols = ['{}{}'.format(x, y) for y in range(1, 6) for x in df.columns]
print cols
['id1', 'value1', 'id2', 'value2', 'id3', 'value3', 'id4', 'value4', 'id5', 'value5']
df = pd.DataFrame(df.values.reshape(df.shape[0] / 5, 10), columns=cols)
print df
id1 value1 id2 value2 id3 value3 id4 value4 id5 value5
0 1 a 2 b 3 c 4 d 5 e
1 6 f 7 g 8 h 9 i 10 j