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
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
'age':[18, 39, 22, 45],
'price': [1.0, 2.0, 3.0, 4.0],
'color': ['red', 'black', None, 'red']}, index=['first', 'second', 'third', 'forth'])
df.reset_index()
---------------------------------------------------------------
index site age price color
0 first google 18 1.0 red
1 second baidu 39 2.0 black
2 third wiki 22 3.0 None
3 forth pandas 45 4.0 red
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
'age':[18, 39, 22, 45],
'price': [1.0, 2.0, 3.0, 4.0],
'color': ['red', 'black', None, 'red']}, index=['first', 'second', 'third', 'forth'])
df.reset_index(drop=True)
---------------------------------------------------------------------
site age price color
0 google 18 1.0 red
1 baidu 39 2.0 black
2 wiki 22 3.0 None
3 pandas 45 4.0 red