在pandas python中从数据框的底部移动到顶部

时间:2017-03-30 11:42:48

标签: python pandas dataframe

我有一个这样的数据框:

      count  capacity  load  throughput 
Obj1  14       34       NaN     NaN

Obj2  42       NaN       30      51

Obj3  34       NaN       20      15

Obj4  23       44        NaN     NaN

0     25       20        Nan     40

现在我需要将最后一行放在列名称上方。

       0     25       20       Nan     40
            count  capacity   load  throughput 
    Obj1     14       34       NaN     NaN

    Obj2     42       NaN       30      51

    Obj3     34       NaN       20      15

    Obj4     23       44       NaN     NaN

这个null左上角可以保留,但没有必要。 它甚至可能吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要iloc来选择最后一行以及删除:

df.columns = [df.iloc[-1],df.columns]
df = df.iloc[:-1]
#if need remove columns names
df.columns.names = (None, None)
print (df)
        25     20.0  Nan       40.0
     count capacity load throughput
Obj1    14     34.0  NaN        NaN
Obj2    42      NaN   30       51.0
Obj3    34      NaN   20       15.0
Obj4    23     44.0  NaN        NaN