參考鏈接:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html#pandas-dataframe-reset-index
DataFrame.
reset_index
(level=None, drop=False, inplace=False, col_level=0, col_fill='')[source]
Reset the index, or a level of it.
Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.
- Parameters
-
- level int, str, tuple, or list, default None
-
Only remove the given levels from the index. Removes all levels by default.
- drop bool, default False
-
Do not try to insert index into dataframe columns. This resets the index to the default integer index.
- inplace bool, default False
-
Modify the DataFrame in place (do not create a new object).
- col_level int or str, default 0
-
If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first level.
- col_fill object, default ‘’
-
If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated.
- Returns
-
- DataFrame or None
-
DataFrame with the new index or None if
inplace=True
.
簡單來看,就是將索引變成columns內容,col_level與col_fill用在列名為多層索引的。
示例代碼
In [59]: df Out[59]: class max_speed falcon bird 389.0 parrot bird 24.0 lion mammal 80.5 monkey mammal NaN In [60]: df.reset_index() Out[60]: index class max_speed 0 falcon bird 389.0 1 parrot bird 24.0 2 lion mammal 80.5 3 monkey mammal NaN In [61]: df.reset_index(drop=True) Out[61]: class max_speed 0 bird 389.0 1 bird 24.0 2 mammal 80.5 3 mammal NaN In [62]:
上面演示了普通的用法,一個是將index編程了列內容,一個是將index刪除了,都用了默認的數字index
后面演示多層索引的示例。
默認情況下,reset_index將會還原所有的索引
In [62]: index = pd.MultiIndex.from_tuples([('bird', 'falcon'), ...: ('bird', 'parrot'), ...: ('mammal', 'lion'), ...: ('mammal', 'monkey')], ...: names=['class', 'name']) ...: columns = pd.MultiIndex.from_tuples([('speed', 'max'), ...: ('species', 'type')]) ...: df = pd.DataFrame([(389.0, 'fly'), ...: ( 24.0, 'fly'), ...: ( 80.5, 'run'), ...: (np.nan, 'jump')], ...: index=index, ...: columns=columns) In [63]: df Out[63]: speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump In [64]: df.reset_index() Out[64]: class name speed species max type 0 bird falcon 389.0 fly 1 bird parrot 24.0 fly 2 mammal lion 80.5 run 3 mammal monkey NaN jump
通過第一個參數的level的設置columns,可以指定需要還原的multiindex的名稱
In [75]: df Out[75]: speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump In [76]: df.reset_index(level='class') Out[76]: class speed species max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump In [77]: df.reset_index(level='name') Out[77]: name speed species max type class bird falcon 389.0 fly bird parrot 24.0 fly mammal lion 80.5 run mammal monkey NaN jump In [78]: df.reset_index(level='name', col_level=1) Out[78]: speed species name max type class bird falcon 389.0 fly bird parrot 24.0 fly mammal lion 80.5 run mammal monkey NaN jump In [79]: df.reset_index(level='class', col_level=0, col_fill='type') Out[79]: class speed species type max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
col_fill參數為設置給mutil_index的默認項