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]: data
Out[307]:
a b c d
0 bar one z 1.0
1 bar two y 2.0
2 foo one x 3.0
3 foo two w 4.0 In [308]: indexed1 = data.set_index('c') In [309]: indexed1
Out[309]:
a b d
c
z bar one 1.0
y bar two 2.0
x foo one 3.0
w foo two 4.0 In [310]: indexed2 = data.set_index(['a', 'b']) In [311]: indexed2
Out[311]:
c d
a b
bar one z 1.0
two y 2.0
foo 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]: data
Out[318]:
c d
a b
bar one z 1.0
two y 2.0
foo one x 3.0
two w 4.0 In [319]: data.reset_index()
Out[319]:
a b c d
0 bar one z 1.0
1 bar two y 2.0
2 foo one x 3.0
3 foo two w 4.0

 

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

pandas set_index和reset_index的用法的更多相关文章

  1. Pandas高级教程之:GroupBy用法

    Pandas高级教程之:GroupBy用法 目录 简介 分割数据 多index get_group dropna groups属性 index的层级 group的遍历 聚合操作 通用聚合方法 同时使用 ...

  2. pandas的set_index和reset_index方法

    import pandas as pd data = pd.DataFrame(np.arange(1,10).reshape(3,3),index=["a","b&qu ...

  3. pandas set_index() reset_index()

    set_index() 官方定义: 使用一个或多个现有列设置索引,   默认情况下生成一个新对象 DataFrame.set_index(keys, drop=True, append=False,  ...

  4. 区别 |python-pandas库set_index、reset_index用法区别

    1.set_index() 作用:DataFrame可以通过set_index方法,将普通列设置为单索引/复合索引. 格式:DataFrame.set_index(keys, drop=True, a ...

  5. set_index()与reset_index()函数

    一 set_index()函数 1 主要是理解drop和append参数,注意与reset_index()参数的不同. import pandas as pd df = pd.DataFrame({' ...

  6. pandas中的reset_index()

    数据清洗时,会将带空值的行删除,此时DataFrame或Series类型的数据不再是连续的索引,可以使用reset_index()重置索引. import pandas as pd import nu ...

  7. Pandas中关于 loc \ iloc 用法的理解

    转载至:https://blog.csdn.net/w_weiying/article/details/81411257 loc函数:通过行索引 "Index" 中的具体值来取行数 ...

  8. Pandas:loc iloc ix用法

    参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...

  9. python set_index与reset_index的妙用

随机推荐

  1. IDA resources - Script, Plugin, Project, Book, Tutorial

    https://www.hex-rays.com/forum/viewtopic.php?f=6&t=3322 List of scripts:http://www.openrce.org/d ...

  2. How to create a Maven web app and deploy to Tomcat - fast

    原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html Procedure Prerequisites and Assu ...

  3. 应对Memcached缓存失效,导致高并发查询DB的几种思路

    原文地址: http://blog.csdn.net/hengyunabc/article/details/20735701 当Memcached缓存失效时,容易出现高并发的查询DB,导致DB压力骤然 ...

  4. Android源码大放送之material design类型

    本文转载自:http://www.apkbus.com/android-243232-1-1.html 鉴于大家对源码的渴望,就算自己辛苦一点也要满足大家的需求,查看了几百个源码之后终于筛选出了这些精 ...

  5. Workflow:采用坐标变换(移动和旋转)画箭头

    背景 流程设计器的连线部分需要画一个箭头代表连接的方向,下图是期望的效果: 刚开始我准备采用三角函数(sin和cos)来计算三角的坐标,实现的过程真不爽(有兴趣的朋友可以试试),就在完工的时候,突然想 ...

  6. [翻译] AnimatedTransitionGallery

    AnimatedTransitionGallery 转场动画回廊 https://github.com/shu223/AnimatedTransitionGallery Collection of i ...

  7. tyvj 2075 借教室 题解

    P2075 [NOIP2012T5]借教室 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 noip2012-tg 描述 在大学期间,经常需要租借教室.大到院 ...

  8. Proxmark3介绍

    Proxmark3介绍 Proxmark3是由Jonathan Westhues设计并且开发的开源硬件,其主要用RFID的嗅探.读取以及克隆等的操作. 其官方网站为:Jonathan Westhues ...

  9. ArrayList的使用和List<T>的比较

    使用非泛型集合类的限制可以通过编写一小段程序来演示,该程序利用 .NET Framework 基类库中的 ArrayList 集合类.ArrayList 是一个使用起来非常方便的集合类,无需进行修改即 ...

  10. 使用Js获取和更改FCKeditor编辑器里的内容

    之前在一个系统里使用了FCKeditor编辑器,由于项目需求需要在FCKeditor里添加一个自定义的按钮用于实现自己的需求 主要是在点击该按钮时删除或添加FCKeditor编辑器里的内容 其实是一个 ...