更改数据帧的索引号 [英] Change the index number of a dataframe

查看:158
本文介绍了更改数据帧的索引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据帧完成一些操作之后,我得到了一个结果数据帧。但是索引没有正确列出如下。

  MsgType / Cxr NoOfMsgs AvgElpsdTime(ms)
161 AM 86 30.13
171 CM 1 104
18 CO 27 1244.81
19 US 23 1369.61
20 VK 2 245
21 VS 11 1273.82
112 fqa 78 1752.22
24 SN 78 1752.22

我想得到如下所示的结果。

  MsgType / Cxr NoOfMsgs AvgElpsdTime(ms)
1 AM 86 30.13
2 CM 1 104
3 CO 27 1244.81
4 US 23 1369.61
5 VK 2 245
6 VS 11 1273.82
7 fqa 78 1752.22
8 SN 78 1752.22

请指导我如何得到这个?

解决方案

这些是$您的数据框的c $ c> rownames ,默认情况下为 1:nrow(dfr)。当您重新排序数据帧时,原始的rownames也被重新排序。要按顺序列出新订单的行,只需使用:

  rownames(dfr)<  -  1:nrow(dfr )


After I'm done with some manipulation in Dataframe, I got a result dataframe. But the index are not listed properly as below.

                    MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
    161                   AM       86            30.13
    171                   CM        1              104
    18                    CO       27          1244.81
    19                    US       23          1369.61
    20                    VK        2              245
    21                    VS       11          1273.82
    112                  fqa       78          1752.22
    24                    SN       78          1752.22

I would like to get the result as like below.

                    MsgType/Cxr NoOfMsgs AvgElpsdTime(ms)
    1                   AM        86            30.13
    2                   CM         1              104
    3                    CO       27          1244.81
    4                    US       23          1369.61
    5                    VK        2              245
    6                    VS       11          1273.82
    7                   fqa       78          1752.22
    8                    SN       78          1752.22

Please guide how I can get this ?

解决方案

These are the rownames of your dataframe, which by default are 1:nrow(dfr). When you reordered the dataframe, the original rownames are also reordered. To have the rows of the new order listed sequentially, just use:

rownames(dfr) <- 1:nrow(dfr)

这篇关于更改数据帧的索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆