将熊猫列变成行

时间:2019-12-04 10:56:13

标签: python pandas

我有一个看起来像这样的数据框:

df = pd.DataFrame({"t":[1,2,3,4,5,6,7,8,9],
                   "b_val":[1,2,3,4,5,6,7,8,9],
                   "c_val":[10,20,30,40,50,60,70,80,90]})

我想将最后两列转换为行,以便数据框如下所示:

pd.DataFrame({"t":[1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9],
              'id':["b","b","b","b","b","b","b","b","b", "c", "c", "c", "c", "c", "c", "c","c", "c"],
              'val':[1,2,3,4,5,6,7,8,9, 10,20,30,40,50,60,70,80,90]})

是否有内置的pandas函数可以做到这一点?

0 个答案:

没有答案