pandas的索引操作
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,Pandas的索引操作索引对象Index1. Series和DataFrame中的索引都是Index对象示例代码:print(type(ser_obj.index))print(type(df_obj
千家信息网最后更新 2025年02月03日pandas的索引操作
Pandas的索引操作
索引对象Index
1. Series和DataFrame中的索引都是Index对象
示例代码:
print(type(ser_obj.index))print(type(df_obj2.index))print(df_obj2.index)
运行结果:
Int64Index([0, 1, 2, 3], dtype='int64')
2. 索引对象不可变,保证了数据的安全
示例代码:
# 索引对象不可变df_obj2.index[0] = 2
运行结果:
---------------------------------------------------------------------------TypeError Traceback (most recent call last) in () 1 # 索引对象不可变----> 2 df_obj2.index[0] = 2/Users/Power/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py in __setitem__(self, key, value) 1402 1403 def __setitem__(self, key, value):-> 1404 raise TypeError("Index does not support mutable operations") 1405 1406 def __getitem__(self, key):TypeError: Index does not support mutable operations
常见的Index种类
- Index, 索引
- Int64Index, 整数索引
- MultiIndex, 层级索引
- DatatimeIndex, 时间戳类型
Series索引
1. index指定行索引名
示例代码:
ser_obj = pd.Series(range(5), index = ['a', 'b', 'c', 'd', 'e'])print(ser_obj.head())
运行结果:
a 0b 1c 2d 3e 4dtype: int64
2. 行索引
ser_obj['label'], ser_obj[pos]
示例代码:
print(ser_obj['b'])print(ser_obj[2])
运行结果:
12
3. 切片索引
ser_obj[2:4], ser_obj['label1':'label3']
注意, 按索引名切片操作时,时包含终止索引的
示例代码:
print(ser_obj[1:3])print(ser_obj['b':'d'])
运行结果:
b 1c 2dtype: int64b 1c 2d 3dtype: int64
4. 不连续索引
ser_obj[['label1', 'label2', 'label3']]
示例代码:
print(ser_obj[[0, 2, 4]])print(ser_obj[['a', 'e']])
运行结果:
a 0c 2e 4dtype: int64a 0e 4dtype: int64
5. 布尔索引
示例代码:
ser_bool = ser_obj > 2print(ser_bool)print(ser_obj[ser_bool])print(ser_obj[ser_obj > 2])
运行结果:
a Falseb Falsec Falsed Truee Truedtype: boold 3e 4dtype: int64d 3e 4dtype: int64
DataFrame索引
1. columns指定列索引名
示例代码:
import numpy as npdf_obj = pd.DataFrame(np.random.randn(5, 4), columns = ['a', 'b', 'c', 'd'])print(df_obj.head())
运行结果:
a b c d0 -0.241678 0.621589 0.843546 -0.3831051 -0.526918 -0.485325 1.124420 -0.6531442 -1.074163 0.939324 -0.309822 -0.2091493 -0.716816 1.844654 -2.123637 -1.3234844 0.368212 -0.910324 0.064703 0.486016
DataFrame索引
Colum index (df.columns) Row index(df.index) a b c d 0 -0.241678 0.621589 0.843546 -0.383105 1 -0.526918 -0.485325 1.124420 -0.653144 2 -1.074163 0.939324 -0.309822 -0.209149 3 -0.716816 1.844654 -2.123637 -1.323484 4 0.368212 -0.910324 0.064703 0.486016
2. 列索引
df_obj[['label']]
示例代码:
print(df_obj['a']) # 返回Series类型print(df_obj[[0]])# 返回DataFrame类型,ipython3中不支持print(type(df_obj[[0]])) # 返回DataFrame类型,ipython3中不支持
运行结果:
0 -0.2416781 -0.5269182 -1.0741633 -0.7168164 0.368212Name: a, dtype: float64
3. 不连续索引
df_obj[['label1', 'label2']]
示例代码:
print(df_obj[['a', 'c']])print(df_obj[[1, 3]]) # ipython3中不支持
运行结果:
a c0 -0.241678 0.8435461 -0.526918 1.1244202 -1.074163 -0.3098223 -0.716816 -2.1236374 0.368212 0.064703 b d0 0.621589 -0.3831051 -0.485325 -0.6531442 0.939324 -0.2091493 1.844654 -1.3234844 -0.910324 0.486016
高级索引:标签、位置和混合
Pandas的高级索引有3种
1. loc标签索引
DataFrame不能直接切片,可以通过loc来做切片
loc是基于标签名的索引,也就是我们自定义的索引名
示例代码:
# 标签索引 loc# Seriesprint(ser_obj['b':'d'])print(ser_obj.loc['b':'d'])# DataFrameprint(df_obj['a'])# 第一个参数索引行,第二个参数是列print(df_obj.loc[0:2, 'a'])
运行结果:
b 1c 2d 3dtype: int64b 1c 2d 3dtype: int640 -0.2416781 -0.5269182 -1.0741633 -0.7168164 0.368212Name: a, dtype: float640 -0.2416781 -0.5269182 -1.074163Name: a, dtype: float64
2. iloc位置索引
作用和loc一样,不过是基于索引编号来索引
示例代码:
# 整型位置索引 iloc# Seriesprint(ser_obj[1:3])print(ser_obj.iloc[1:3])# DataFrameprint(df_obj.iloc[0:2, 0]) # 注意和df_obj.loc[0:2, 'a']的区别
运行结果:
b 1c 2dtype: int64b 1c 2dtype: int640 -0.2416781 -0.526918Name: a, dtype: float64
3. ix标签与位置混合索引
ix是以上二者的综合,既可以使用索引编号,又可以使用自定义索引,要视情况不同来使用,
如果索引既有数字又有英文,那么这种方式是不建议使用的,容易导致定位的混乱。
示例代码:
# 混合索引 ix# Seriesprint(ser_obj.ix[1:3])print(ser_obj.ix['b':'c'])#DataFrameprint(df_obj.loc[0:2, 'a'])print(df_obj.ix[0:2, 0])
运行结果
b 1c 2dtype: int64b 1c 2dtype: int640 -0.2416781 -0.5269182 -1.074163Name: a, dtype: float64
注意
DataFrame索引操作,可将其看作ndarray的索引操作
标签的切片索引是包含末尾位置的
索引
代码
示例
结果
运行
位置
对象
标签
类型
可变
支持
混合
高级
参数
列索
不同
安全
混乱
也就是
作用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海运营网络技术参考价格
开元棋牌服务器
网络安全警示教育片完整
幻塔为什么看不了其他服务器
网络安全知识竞赛的优点
冈网上网络安全
网络安全意识调查问卷
紫青双剑苹果服务器
区块链系统和数据库
互联网和黑科技材料的汇编
警告服务器在美国
网络安全绘画教程眼睛
手机的软件开发包括哪些
数据库通过不同驱动连接
大学怎么学好数据库
数据库基础操作的实验心得
宠物大战服务器大全
无锡运营软件开发代理品牌
吉他谱软件开发
微信小程序数据库创建图片数组
网络安全工作的工作亮点
合成图片的手机软件开发
株洲it软件开发工程师短期班
数据库安全审计系统 通俗易懂
服务器最安全关机命令
保险软件开发前景
管理我的世界服务器
软件开发是啥行业
猫耳庄园服务器群号
奉贤区营销软件开发销售