Lesson8——Pandas reindex重置索引
1 简介
重置索引(reindex)可以更改原 DataFrame 的行标签或列标签,并使更改后的行、列标签与 DataFrame 中的数据逐一匹配。通过重置索引操作,您可以完成对现有数据的重新排序。如果重置的索引标签在原 DataFrame 中不存在,那么该标签对应的元素值将全部填充为 NaN。
2 重置行列标签
选取特定行、列。
示例:先构建数据
index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror']
df = pd.DataFrame({'http_status': [200, 200, 404, 404, 301],
'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]},
index=index)
df
输出结果:
http_status response_time
Firefox 200 0.04
Chrome 200 0.02
Safari 404 0.07
IE10 404 0.08
Konqueror 301 1.00
示例:同时使用行、列标签选取数据。
new_index = ['Firefox', 'IE10', 'Safari']
df.reindex(index=new_index,columns=['response_time'])
输出结果:
response_time
Firefox 0.04
IE10 0.08
Safari 0.07
示例:只使用行标签选取数据。
new_index = ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10',
'Chrome']
df.reindex(new_index)
输出结果:不存在的行使用 NaN 代替。
http_status response_time
Safari 404.0 0.07
Iceweasel NaN NaN
Comodo Dragon NaN NaN
IE10 404.0 0.08
Chrome 200.0 0.02
现有 a、b 两个 DataFrame 对象,如果想让 a 的行索引与 b 相同,您可以使用 reindex_like() 方法。
示例如下:
a = pd.DataFrame(np.arange(6).reshape((2,3)),columns=['col1','col2','col3'])
b = pd.DataFrame(np.arange(12).reshape((4,3)),columns=['col1','col2','col3'])
a.reindex_like(b)
输出结果:由于 a 的 size 小于 b ,所以 2 、3行不存在,用 NaN 代替。
col1 col2 col3
0 0.0 1.0 2.0
1 3.0 4.0 5.0
2 NaN NaN NaN
3 NaN NaN NaN
示例:
b.reindex_like(a)
输出结果:
col1 col2 col3
0 0 1 2
1 3 4 5
3 填充元素值
reindex_like() 提供了一个可选的参数 method
,使用它来填充相应的元素值,参数值介绍如下:
- pad/ffill:向前填充值;
- bfill/backfill:向后填充值;
- nearest:从距离最近的索引值开始填充。
示例:
a.reindex_like(b,method='ffill')
输出结果:相当于从有数据的最后一行复制数据到下面的每一行。
col1 col2 col3
0 0 1 2
1 3 4 5
2 3 4 5
3 3 4 5
示例:
a.reindex_like(b,method='bfill')
输出结果:相当于从最后一行复制数据到上面的行。
col1 col2 col3
0 0.0 1.0 2.0
1 3.0 4.0 5.0
2 NaN NaN NaN
3 NaN NaN NaN
示例:
a.reindex_like(b,method='nearest')
输出结果:
col1 col2 col3
0 0 1 2
1 3 4 5
2 3 4 5
3 3 4 5
4 限制填充行数
reindex_like() 还提供了一个额外参数 limit,该参数用来控制填充的最大行数。
示例如下:
a.reindex_like(b,method='ffill',limit=1)
输出结果:这里只填充了 1 行。
col1 col2 col3
0 0.0 1.0 2.0
1 3.0 4.0 5.0
2 3.0 4.0 5.0
3 NaN NaN NaN
5 重命名标签
rename() 方法允许您使用某些映射 (dict或Series) 或任意函数来对行、列标签重新命名。
原始数据:df1 =
col1 col2 col3
0 0 1 2
1 3 4 5
示例如下:
df1.rename(columns={'col1':'c1','col2':'c2','col3':'c3'},index={0:'A',1:'B'})
输出结果:
c1 c2 c3
A 0 1 2
B 3 4 5
rename() 方法提供了一个 inplace 参数,默认值为 False,表示拷贝一份原数据,并在复制后的数据上做重命名操作。若 inplace=True 则表示在原数据的基础上重命名。
Lesson8——Pandas reindex重置索引的更多相关文章
- Pandas基本功能之reindex重新索引
重新索引 reindex重置索引,如果索引值不存在,就引入缺失值 参数介绍 参数 说明 index 用作索引的新序列 method 插值 fill_vlaue 引入缺失值时的替代NaN limit 最 ...
- pandas重置索引的几种方法探究
pandas重置索引的几种方法探究 reset_index() reindex() set_index() 函数名字看起来非常有趣吧! 不仅如此. 需要探究. http://nbviewer.jupy ...
- 【pandas】pandas.DataFrame.rename()---重置索引名称
官方文档 github地址 例子: 创建DataFrame ### 导入模块 import numpy as np import pandas as pd import matplotlib.pypl ...
- Pandas | 08 重建索引
重新索引会更改DataFrame的行标签和列标签. 可以通过索引来实现多个操作: 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置插入缺失值(NA)标记. import pandas a ...
- pandas基础用法——索引
# -*- coding: utf-8 -*- # Time : 2016/11/28 15:14 # Author : XiaoDeng # version : python3.5 # Softwa ...
- pandas中层次化索引与切片
Pandas层次化索引 1. 创建多层索引 隐式索引: 常见的方式是给dataframe构造函数的index参数传递两个或是多个数组 Series也可以创建多层索引 Series多层索引 B =Ser ...
- pandas 之 时间序列索引
import numpy as np import pandas as pd 引入 A basic kind of time series object in pandas is a Series i ...
- pandas 之 多层索引
In many applications, data may be spread across a number of files or datasets or be arranged in a fo ...
- REINDEX - 重建索引
SYNOPSIS REINDEX { DATABASE | TABLE | INDEX } name [ FORCE ] DESCRIPTION 描述 REINDEX 基于存储在表上的数据重建索引, ...
随机推荐
- Hamburger Magi(hdu 3182)
Hamburger Magi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- matplotlib 进阶之Tight Layout guide
目录 简单的例子 Use with GridSpec Legend and Annotations Use with AxesGrid1 Colorbar 函数链接 matplotlib教程学习笔记 ...
- 基于Spring MVC + Spring + MyBatis的【物流系统 - 公司信息管理】
资源下载:https://download.csdn.net/download/weixin_44893902/45601768 练习点设计:模糊查询.删除.新增 一.语言和环境 实现语言:JAVA语 ...
- Jedis 基本使用
引入 jedis 依赖: <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <!-- Dec 15, 2 ...
- django后台admin页面表单自定义
自定义一个form 表单来替换admin默认的表单 在自定义表单中可以定义字段和验证 https://docs.djangoproject.com/zh-hans/3.2/ref/contrib/ad ...
- Selenium_使用switch_to.window方法处理窗口切换(12)
想一下这样的场景,打开页面A点击一个链接,在一个新的窗口打开页面B,由于之前的driver实例对象在页面A,但是你接下来操作的元素在页面B中,此时脚本就会报错找不到元素.该场景需要使用到seleniu ...
- CentOS7添加开机启动服务或脚本
方法一(rc.local) 改方式配置自动启动最为简单,只需要修改rc.local文件 由于在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod + ...
- CentOS7中安装pip的方法
1.安装epel-release [root@localhost ~]# yum -y install epel-release 2.安装python-pip [root@localhost ~]# ...
- 图形验证插件,百度编辑器拓展功能,NodeJs消息机制以及聊天室
图形验证插件 网上找了很多图形验证插件,比较推荐verify.js <link rel="stylesheet" type="text/css" href ...
- Linux 安装 MySQL 8.0.26 超详细图文步骤
1.MySQL 8.0.26 下载 官方网站下载 MySQL 8.0.26 安装包,下载地址: https://downloads.mysql.com/archives/community/ 需要注意 ...