Pandas | 08 重建索引
重新索引会更改DataFrame的行标签和列标签。
可以通过索引来实现多个操作:
- 重新排序现有数据以匹配一组新的标签。
- 在没有标签数据的标签位置插入缺失值(NA)标记。
import pandas as pd
import numpy as np N=20 df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
print(df)
print('\n') #reindex the DataFrame
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B']) # 将符合的提取出来了
print (df_reindexed)
输出结果:
A x y C D
0 2016-01-01 0.0 0.910736 Low 105.308796
1 2016-01-02 1.0 0.570500 Low 91.024238
2 2016-01-03 2.0 0.930298 High 112.359308
3 2016-01-04 3.0 0.251355 Medium 106.155192
4 2016-01-05 4.0 0.579235 Low 90.079651
5 2016-01-06 5.0 0.623852 High 110.592218
6 2016-01-07 6.0 0.621130 Medium 96.222673
7 2016-01-08 7.0 0.989647 Medium 92.253444
8 2016-01-09 8.0 0.506653 Medium 102.601417
9 2016-01-10 9.0 0.099482 Low 97.721659
10 2016-01-11 10.0 0.254750 Medium 75.502131
11 2016-01-12 11.0 0.543014 Medium 88.895951
12 2016-01-13 12.0 0.911283 Medium 79.526056
13 2016-01-14 13.0 0.255296 Low 92.248119
14 2016-01-15 14.0 0.205302 Low 103.301747
15 2016-01-16 15.0 0.246407 Low 107.158250
16 2016-01-17 16.0 0.202039 High 96.411279
17 2016-01-18 17.0 0.734529 High 88.177103
18 2016-01-19 18.0 0.275703 Medium 82.885365
19 2016-01-20 19.0 0.084449 High 98.803349 A C B
0 2016-01-01 Low NaN
2 2016-01-03 High NaN
5 2016-01-06 High NaN
重建索引与其他对象对齐
有时可能希望采取一个对象和重新索引,其轴被标记为与另一个对象相同。 考虑下面的例子来理解这一点。
import pandas as pd
import numpy as np df1 = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3'])
print(df1)
print(df2) df1 = df1.reindex_like(df2) # 在df1中,把和df2一样的标签行提取出来
print(df1)
输出结果:
col1 col2 col3
0 0.989992 0.543438 -2.311684
1 -0.704759 -0.555589 -0.570049
2 -0.658263 -0.605368 -0.025520
3 1.533949 -0.936191 -0.071094
4 -0.729812 -0.339670 0.468700
5 -0.164076 0.075098 0.654549
6 -0.491034 1.096496 -0.166250
7 0.230918 -1.561643 1.501326
8 0.703623 -0.407445 -0.792633
9 0.340817 -1.132127 -0.695821 col1 col2 col3
0 0.144380 0.295776 -0.743097
1 -1.597853 0.029949 -1.605222
2 0.626728 -0.077997 -0.167353
3 0.466008 0.695279 -0.047752
4 -1.088821 -0.456605 1.192847
5 -0.020330 1.616297 -0.368196
6 -1.038790 -1.264894 0.059060 col1 col2 col3
0 0.989992 0.543438 -2.311684
1 -0.704759 -0.555589 -0.570049
2 -0.658263 -0.605368 -0.025520
3 1.533949 -0.936191 -0.071094
4 -0.729812 -0.339670 0.468700
5 -0.164076 0.075098 0.654549
6 -0.491034 1.096496 -0.166250
注意 - 在这里,
df1
数据帧(DataFrame)被更改并重新编号,如df2
。 列名称应该匹配,否则将为整个列标签添加NAN
。
填充时重新加注
reindex()
采用可选参数方法,它是一个填充方法,其值如下:
pad/ffill
- 向前填充值bfill/backfill
- 向后填充值nearest
- 从最近的索引值填充
import pandas as pd
import numpy as np df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3']) # Padding NAN's
print(df2.reindex_like(df1))
print('\n') # Now Fill the NAN's with preceding Values
print ("Data Frame with Forward Fill:")
print (df2.reindex_like(df1,method='ffill'))
输出结果:
col1 col2 col3
0 1.311620 -0.707176 0.599863
1 -0.423455 -0.700265 1.133371
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
Data Frame with Forward Fill:
col1 col2 col3
0 1.311620 -0.707176 0.599863
1 -0.423455 -0.700265 1.133371
2 -0.423455 -0.700265 1.133371
3 -0.423455 -0.700265 1.133371
4 -0.423455 -0.700265 1.133371
5 -0.423455 -0.700265 1.133371
注 - 最后四行被填充了。
重建索引时的填充限制
限制参数在重建索引时提供对填充的额外控制。限制指定连续匹配的最大计数。
import pandas as pd
import numpy as np df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3']) # Padding NAN's
print(df2.reindex_like(df1))
print('\n') # Now Fill the NAN's with preceding Values
print ("Data Frame with Forward Fill limiting to 1:")
print(df2.reindex_like(df1,method='ffill',limit=1))
输出结果:
col1 col2 col3
0 0.247784 2.128727 0.702576
1 -0.055713 -0.021732 -0.174577
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
Data Frame with Forward Fill limiting to 1:
col1 col2 col3
0 0.247784 2.128727 0.702576
1 -0.055713 -0.021732 -0.174577
2 -0.055713 -0.021732 -0.174577
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
注意 - 只有第
7
行由前6
行填充。 然后,其它行按原样保留。
重命名
rename()
方法允许基于一些映射(字典或者系列)或任意函数来重新标记一个轴。
import pandas as pd
import numpy as np df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
print(df1)
print('\n') print ("After renaming the rows and columns:")
print(df1.rename(columns={'col1' : 'c1', 'col2' : 'c2'},index = {0 : 'apple', 1 : 'banana', 2 : 'durian'}))
输出结果:
col1 col2 col3
0 0.486791 0.105759 1.540122
1 -0.990237 1.007885 -0.217896
2 -0.483855 -1.645027 -1.194113
3 -0.122316 0.566277 -0.366028
4 -0.231524 -0.721172 -0.112007
5 0.438810 0.000225 0.435479
After renaming the rows and columns:
c1 c2 col3
apple 0.486791 0.105759 1.540122
banana -0.990237 1.007885 -0.217896
durian -0.483855 -1.645027 -1.194113
3 -0.122316 0.566277 -0.366028
4 -0.231524 -0.721172 -0.112007
5 0.438810 0.000225 0.435479
rename()
方法提供了一个inplace
命名参数,默认为False
并复制底层数据。 指定传递inplace = True
则表示将数据重命名。
Pandas | 08 重建索引的更多相关文章
- Pandas重建索引
重新索引会更改DataFrame的行标签和列标签.重新索引意味着符合数据以匹配特定轴上的一组给定的标签. 可以通过索引来实现多个操作 - 重新排序现有数据以匹配一组新的标签. 在没有标签数据的标签位置 ...
- SQLServer 重建索引前后对比
在做维护项目的时,我们经常会遇到索引维护的问题,通过语句,我们就可以判断某个表的索引是否需要重建. 执行一下语句:先分析表的索引 分析表的索引建立情况:DBCC showcontig('Table') ...
- 重建索引解决mssql表查询超时的问题
表已有数据,150万+,执行一个group by 的查询出现超时,一个一个条件减少尝试,前几个where条件不超时,而在加上最后一个条件时就超时了. 分析表的索引建立情况:DBCC showconti ...
- SQLServer2005重建索引前后对比【转】
在做维护项目的时,我们经常会遇到索引维护的问题,通过语句,我们就可以判断某个表的索引是否需要重建. 执行一下语句:先分析表的索引 分析表的索引建立情况:DBCC showcontig('Table') ...
- Sql 查询过慢,尝试重建索引
DBCC showcontig('Table') DBCC DBREINDEX('Table') 分析表的索引建立情况:DBCC showcontig('Table') DBCC SHOWCONTIG ...
- 重建索引提高SQL Server性能
大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数 ...
- DBCC DBREINDEX重建索引提高SQL Server性能
大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数 ...
- SQL Server 2012 批量重建索引
关于索引的概念可以看看宋大牛的博客 T-SQL查询高级—SQL Server索引中的碎片和填充因子 整个数据库的索引很多,索引碎片多了,不可能一个个的去重建,都是重复性的工作,所以索性写了个存储过程, ...
- SQL Server重建索引计划
每周日2点进行”一致性检查“ 每周六1点进行”重建索引“,重建索引会自动完成更新统计信息操作
随机推荐
- etcd v3 ssl 集群添加新节点
集群搭建 下面只用同一台服务器进行三个成员节点的开启 节点1 ./etcd --name cd0 --initial-advertise-peer-urls http://127.0.0.1:2380 ...
- HTML ------- 对文本进行操作的元素
1.HTML 标题(Heading) 在<h1> -- <h6> 标签进行定义,<h1>定义最大标题,<h6>定义最小的标题 作用:标题会自动加粗,大 ...
- 转 Java jar (SpringBoot Jar)转为win可执行的exe程序
原文链接:http://voidm.com/2018/12/29/java-jar-transform-exe/打包Jar工程 将java项目打包成jar工程,可以是文章以SpringBoot为例po ...
- 如何防止短信API接口遍历
短信API接口在web中得到越来越多的应用,如用户注册,登录,密码重置等业务模块都会使用手机验证码进行身份验证.一般情况下,我们会采用这样的安全策略,将短信发送频率限制在正常的业务流控范围内,比如,一 ...
- dotnet中文字符工具类
支持繁体简体互换. using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- 使用Net Mail发送邮件
最近用到了发送邮件这个功能,简单记录一下案例.代码如下: using System; using System.Collections.Generic; using System.Linq; usin ...
- 简单的sql语句汇总(sqlserver)
1.修改字段的默认值 alter table 表名 add default 默认值 for 字段名称 例子: for Age; alter table 表名 add constraint DF_TAB ...
- PIE SDK直方图统计法
1.算法功能简介 图像直方图描述了图像中每个亮度值DN的像元数量的统计分布.它通过每个亮度值的像元数除以图像中总得像元数,即频率直方图.在很多遥感应用中,直方图是遥感图像中所包含的信息的一种有用的图示 ...
- vue.js 打包时出现空白页和路径错误
vue-cli输入命令:npm run build 即可打包vue.js的项目 打包出来后项目中就会多了一个文件夹dist,下图为我们打包过后的项目 我们直接运行打包后的文件夹中的index.ht ...
- MySql定时备份脚本
最近需要对某服务的数据库数据进行备份,因此参考网上教程完成数据库备份脚本. 因为服务的使用频率较低,因此设置定时任务,在每天的中午以及午夜时分进行备份操作. #!/bin/bash # 设置mysql ...