简体   繁体   English

如何在数据框中引入新的索引值?

[英]How to introduce new index values in a data frame?

I have the following dataframe, which the print(df_74034.head(10)) is: 我有以下数据print(df_74034.head(10))print(df_74034.head(10))是:

                          HumExamRoom                      HumTechRoom Machine  TempExamRoom TempTechRoom
                                   NaN  2017-08-18\t09:22:33\t65.9   74034           NaN          NaN
                                   NaN  2017-10-03\t11:08:26\t60.5   74034           NaN          NaN
2018-02-17\t01:45:24          NaN                             69.7   74034           NaN          NaN
2018-02-17\t01:45:55          NaN                             67.5   74034           NaN          NaN
2018-02-17\t01:46:29          NaN                             65.4   74034           NaN          NaN
2018-02-17\t01:47:20          NaN                             63.3   74034           NaN          NaN
2018-02-17\t01:48:35          NaN                             61.3   74034           NaN          NaN
2018-02-17\t01:49:08          NaN                             63.4   74034           NaN          NaN
2018-02-17\t01:49:31          NaN                             65.5   74034           NaN          NaN
2018-02-17\t01:49:55          NaN                             67.6   74034           NaN          NaN

(The first two rows got a little messy) As you can see, the first two cells in the index have no values at all because their values are in the second column, which is probably a fault of my code - which I don't want to change anymore. (前两行有点杂乱)正如你所看到的,索引中的前两个单元格根本没有值,因为它们的值在第二列中,这可能是我的代码的错误 - 我不知道想要改变了。 Therefore, I would like to introduce the values 2017-08-18\\t09:22:33\\t and `2017-08-18\\t09:22:33\\t' in the index cells, respectively, and sort it. 因此,我想分别在索引单元格中引入值2017-08-18\\t09:22:33\\t和“2017-08-18 \\ t09:22:33 \\ t”,并对其进行排序。 In order to sort I also have to change it to its correct date/time format. 为了排序我还必须将其更改为正确的日期/时间格式。

I've already tried df_74034.index.insert(0, '2017-08-18\\t09:22:33') but it didn't work. 我已经尝试过df_74034.index.insert(0, '2017-08-18\\t09:22:33')但它没有用。 I appretiate for the help! 我乐意为你提供帮助!

You can perform it in other way - 你可以用其他方式执行它 -

df = dataframe name which in your case is df_74034. df =数据帧名称,在您的情况下为df_74034。

  1. You may try df.reset_index() which will place the current index values with integer number and will create column of your existing index value. 您可以尝试df.reset_index(),它将使用整数放置当前索引值,并将创建现有索引值的列。
  2. Create a new column of the values you want to set as index. 创建要设置为索引的值的新列。 Lets say name of the column is ABC 让我们说这个专栏的名字是ABC
  3. Then, you can use df.set_index('ABC',inplace=True) which will create a new index with values those are in ABC column. 然后,您可以使用df.set_index('ABC',inplace = True),这将创建一个新的索引,其值在ABC列中。

I hope it may be helpful to some extend. 我希望它可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM