博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas.DataFrame.reset_index
阅读量:6002 次
发布时间:2019-06-20

本文共 4256 字,大约阅读时间需要 14 分钟。

DataFrame.
reset_index
(
level=None
drop=False
inplace=False
col_level=0
col_fill=''
)

For DataFrame with multi-level index, return new DataFrame with labeling information in the columns under the index names, defaulting to ‘level_0’, ‘level_1’, etc. if any are None. For a standard index, the index name will be used (if set), otherwise a default ‘index’ or ‘level_0’ (if ‘index’ is already taken) will be used.

Parameters:

level : int, str, tuple, or list, default None

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

drop : boolean, default False

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

inplace : boolean, 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:

resetted : DataFrame

Examples

>>> df = pd.DataFrame([('bird',    389.0),...                    ('bird',     24.0),...                    ('mammal',   80.5),...                    ('mammal', np.nan)],...                   index=['falcon', 'parrot', 'lion', 'monkey'],...                   columns=('class', 'max_speed'))>>> df         class  max_speedfalcon    bird      389.0parrot    bird       24.0lion    mammal       80.5monkey  mammal        NaN

When we reset the index, the old index is added as a column, and a new sequential index is used:

>>> df.reset_index()    index   class  max_speed0  falcon    bird      389.01  parrot    bird       24.02    lion  mammal       80.53  monkey  mammal        NaN

We can use the drop parameter to avoid the old index being added as a column:

>>> df.reset_index(drop=True)    class  max_speed0    bird      389.01    bird       24.02  mammal       80.53  mammal        NaN

You can also use reset_index with MultiIndex.

>>> 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)>>> df               speed species                 max    typeclass  namebird   falcon  389.0     fly       parrot   24.0     flymammal lion     80.5     run       monkey    NaN    jump

If the index has multiple levels, we can reset a subset of them:

>>> df.reset_index(level='class')         class  speed species                  max    typenamefalcon    bird  389.0     flyparrot    bird   24.0     flylion    mammal   80.5     runmonkey  mammal    NaN    jump

If we are not dropping the index, by default, it is placed in the top level. We can place it in another level:

>>> df.reset_index(level='class', col_level=1)                speed species         class    max    typenamefalcon    bird  389.0     flyparrot    bird   24.0     flylion    mammal   80.5     runmonkey  mammal    NaN    jump

When the index is inserted under another level, we can specify under which one with the parameter col_fill:

>>> df.reset_index(level='class', col_level=1, col_fill='species')              species  speed species                class    max    typenamefalcon           bird  389.0     flyparrot           bird   24.0     flylion           mammal   80.5     runmonkey         mammal    NaN    jump

If we specify a nonexistent level for col_fill, it is created:

>>> df.reset_index(level='class', col_level=1, col_fill='genus')                genus  speed species                class    max    typenamefalcon           bird  389.0     flyparrot           bird   24.0     flylion           mammal   80.5     runmonkey         mammal    NaN    jump

转载地址:http://wucmx.baihongyu.com/

你可能感兴趣的文章
在红帽的linux-rhce-5.4下配置lamp环境
查看>>
流程控制 - PHP手册笔记
查看>>
cocos2d-x 3.0 新特性样例
查看>>
杭州iPhone电池已排到周五!旧款iPhone换电池各地揪心指数大比拼
查看>>
历经近一个世纪的OCR技术如今怎么样了?
查看>>
在滴滴,我们是怎么做运维的?
查看>>
技术团队里什么样的人会被清除?抢老板的工作干合适吗?
查看>>
马云呼吁快递业绿色升级:“快”保证不输,“绿”才能赢
查看>>
蚂蚁森林上线了9.7KG能量球,50万人争着为它买保护罩
查看>>
“自拍神器”太火了 开售两周销量过百万
查看>>
OFO真没钱了?凤凰自行车索赔6815万 还拖欠云鸟德邦费用
查看>>
双11销售额再刷纪录,新增IT成本却降低一半,阿里又干了什么?
查看>>
十部门发促消费“24条”:提高相对低收入群体待遇
查看>>
「每天一道面试题」CountDownLatch和CyclicBarrier的异同?
查看>>
相机博物馆收藏千件“古董” 有的已跨越一个多世纪
查看>>
澳网今开战:费德勒小威冲击里程碑 王蔷领衔金花
查看>>
除了线上抢购还要上口碑吃大餐!看留学生花式攻略玩转天猫双11
查看>>
探访“全甲格斗”圈 文职员工穿上盔甲就成场上最凶猛的人
查看>>
iOS-VideoToolbox硬编码H264
查看>>
Java处理Excel公式,外部超链接(Url、文件)
查看>>