DataFrame.reset_index

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill=’’)
重置索引,或一个索引级别。

Parameters

level 仅从索引中删除给定的索引级别。默认删除所有级别。
drop 是否尝试将原索引插入到数据框列中
inplace False:返回一个副本;True:就地执行操作并返回None
col_level 如果列有多个级别,则确定将标签插入到哪个级别。默认情况插入到第一级。
col_fill 如果列有多个级别,则确定其他级别的命名方式。如果None则重复索引名称

Example

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']}, index=['first', 'second', 'third', 'forth'])
  6. df.reset_index()
  7. ---------------------------------------------------------------
  8. index site age price color
  9. 0 first google 18 1.0 red
  10. 1 second baidu 39 2.0 black
  11. 2 third wiki 22 3.0 None
  12. 3 forth pandas 45 4.0 red

Example

  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', None, 'red']}, index=['first', 'second', 'third', 'forth'])
  6. df.reset_index(drop=True)
  7. ---------------------------------------------------------------------
  8. site age price color
  9. 0 google 18 1.0 red
  10. 1 baidu 39 2.0 black
  11. 2 wiki 22 3.0 None
  12. 3 pandas 45 4.0 red