pandas.DataFrame.reset_index的使用介绍

参考链接:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html#pandas-dataframe-reset-index

DataFrame.reset_index(level=Nonedrop=Falseinplace=Falsecol_level=0col_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
levelint, str, tuple, or list, default None

Only remove the given levels from the index. Removes all levels by default.

dropbool, default False

Do not try to insert index into dataframe columns. This resets the index to the default integer index.

inplacebool, default False

Modify the DataFrame in place (do not create a new object).

col_levelint 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_fillobject, 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的默认项

原文地址:https://www.cnblogs.com/sidianok/p/14366783.html