您现在的位置是:网站首页> 编程资料编程资料
pandas学习之df.set_index的具体使用_python_
2023-05-26
337人已围观
简介 pandas学习之df.set_index的具体使用_python_
处理数据时,经常需要对索引进行处理,那么可以通过set_index和reset_index来进行处理
官方文档
DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)
参数解释
构建实例
import pandas as pd df = pd.DataFrame(data={'height':[178,171,185,196],'weight':[156,90,140,142], 'name':['小王','小明','小绿','小红']}) df height weight name 0 178 156 小王 1 171 90 小明 2 185 140 小绿 3 196 142 小红key:label array-like or list of label/arrays
需要设置成索引的数据,可以使一个标签,数组,或者标签或数组的列表
df.set_index('name')#指定某一列为索引 height weight name 小王 178 156 小明 171 90 小绿 185 140 小红 196 142drop:bool,default True
是否删除作为索引使用的列,默认True,即删除做为索引的列
df.set_index('name',drop=False) height weight name name 小王 178 156 小王 小明 171 90 小明 小绿 185 140 小绿 小红 196 142 小红append:bool default False
将序列添加到索引中,形成多级序列
df.set_index(df['name'],append = True) height weight name name 0 小王 178 156 小王 1 小明 171 90 小明 2 小绿 185 140 小绿 3 小红 196 142 小红 # 前两列都为索引
inplace:bool default False
将结果返回为原变量
df#原df height weight name 0 178 156 小王 1 171 90 小明 2 185 140 小绿 3 196 142 小红 df.set_index(df['name'],append = True,inplace = True) height weight name name 0 小王 178 156 小王 1 小明 171 90 小明 2 小绿 185 140 小绿 3 小红 196 142 小红 df#无需对df重新赋值,df即为上边代码的结果 height weight name name 0 小王 178 156 小王 1 小明 171 90 小明 2 小绿 185 140 小绿 3 小红 196 142 小红
verify_integrity:bool default False
检查索引是否重复。默认是False。
到此这篇关于pandas学习之df.set_index的具体使用的文章就介绍到这了,更多相关pandas df.set_index内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:
相关内容
- Python实现Gif图片分解的示例代码_python_
- python绘制发散型柱状图+误差阴影时间序列图+双坐标系时间序列图+绘制金字塔图_python_
- Python 多线程爬取案例_python_
- Python爬虫Requests库的使用详情_python_
- Python数据分析matplotlib折线图案例处理_python_
- python time时间库详解_python_
- pandas学习之df.fillna的具体使用_python_
- Python数据分析之使用scikit-learn构建模型_python_
- 利用Python实现普通视频变成动漫视频_python_
- 基于PyQt5实现状态栏(statusBar)显示和隐藏功能_python_
