博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas set_index和reset_index的用法
阅读量:7244 次
发布时间:2019-06-29

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

1.set_index

DataFrame可以通过set_index方法,可以设置单索引和复合索引。 

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False) 
append添加新索引,drop为False,inplace为True时,索引将会还原为列

In [307]: dataOut[307]:      a    b  c    d0  bar  one  z  1.01  bar  two  y  2.02  foo  one  x  3.03  foo  two  w  4.0 In [308]: indexed1 = data.set_index('c') In [309]: indexed1Out[309]:      a    b    dc               z  bar  one  1.0y  bar  two  2.0x  foo  one  3.0w  foo  two  4.0 In [310]: indexed2 = data.set_index(['a', 'b']) In [311]: indexed2Out[311]:          c    da   b          bar one  z  1.0    two  y  2.0foo one  x  3.0    two  w  4.0

  

2.reset_index

reset_index可以还原索引,从新变为默认的整型索引 

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill=”) 
level控制了具体要还原的那个等级的索引 
drop为False则索引列会被还原为普通列,否则会丢失

In [318]: dataOut[318]:          c    da   b          bar one  z  1.0    two  y  2.0foo one  x  3.0    two  w  4.0 In [319]: data.reset_index()Out[319]:      a    b  c    d0  bar  one  z  1.01  bar  two  y  2.02  foo  one  x  3.03  foo  two  w  4.0

 

转自:https://blog.csdn.net/jingyi130705008/article/details/78162758

你可能感兴趣的文章
漫游Kafka设计篇之性能优化
查看>>
JConsole
查看>>
JavaScript初探之——图片移动
查看>>
ABI 管理
查看>>
js22--链式调用
查看>>
列出Windows域中所有的机器
查看>>
C#趣味程序---百鸡百钱
查看>>
原创:微信小程序页面跳转展示缓冲提示
查看>>
mysql学习之四:sql语句学习2
查看>>
Ubuntu14.04下沙盒数据导入到 Neo4j 数据库(图文详解)
查看>>
如何设断点????-----使用WinDbg调试SQL Server查询
查看>>
sql 高性能存储过程分页
查看>>
Java -- 异常的捕获及处理 -- 异常类的继承结构
查看>>
外链建设的主要门户渠道
查看>>
sqlserver如何添加全文索引
查看>>
UVALive - 4960 Sensor network(生成树+LCA)
查看>>
IIS与asp.net管道
查看>>
poj 2585 Window Pains 暴力枚举排列
查看>>
[ACM] ZOJ 3725 Painting Storages (DP计数+组合)
查看>>
Java:String和Date、Timestamp之间的转换
查看>>