在 pandas 中的另一列下方添加整列数据

Adding a entire column data below the other column in pandas

我有一个这样的数据框:

time a   b
0    10  20
1    11  21

现在我需要这样的数据框:

time a
0    10
1    11
0    20
1    21

这可以通过 melt 来完成:

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

输出:

   time   a
0     0  10
1     1  11
2     0  20
3     1  21

或者如果您的数据中有 a,b 以外的列:

df.melt('time', ['a','b'], value_name='a').drop('variable', axis=1)