如何将数据框的所有列合并为一列?

时间:2020-05-07 19:06:06

标签: python pandas

我有一个数据框,其中所有列均名为“标准偏差”,共265列。 我想以这种方式将它们结合在一起:

    a   b   c   d
0   a0  b0  c0  d0
1   a1  b1  c1  d1
2   a2  b2  c2  d2
3   a3  b3  c3  d3
4   a4  b4  c4  d4

becomes 

    A
0   a0
1   a1
2   a2
3   a3
4   a4
5   b0
6   b1
7   b2
8   b3
9   b4
10  c0
11  c1
12  c2
13  c3
14  c4
15  d0
16  d1
17  d2

Stack()以不同的方式组合它们。

我尝试了concat,但收到错误消息:

df = pd.concat(df['Standard Deviation'], ignore_index=True)

>>TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

以下是数据框的外观:

Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  ... Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation  Standard Deviation
0   5.7735e-05  N/A 5.7735e-05  N/A N/A N/A N/A N/A N/A N/A ... N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
1   0.00050914  N/A 0.000337704 N/A 8.27103 N/A N/A N/A N/A N/A ... N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
2   7.61074e-05 N/A 1.35277e-05 N/A 1.73205 N/A N/A N/A N/A N/A ... N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A
3   2.74651e-05 N/A 1.85562e-05 N/A 270 N/A N/A N/A N/A N/A ... N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A

2 个答案:

答案 0 :(得分:1)

您可以融化数据框并删除称为“变量”的标签列。

df.melt().drop('variable', axis=1)

答案 1 :(得分:1)

选项1:

 column = pd.Series(df.values.flatten()) # or df if you want

注意2:

 df.melt().drop('variable',axis=1)