Pandas loc/iloc用法详解
在数据分析过程中,很多时候需要从数据表中提取出相应的数据,而这么做的前提是需要先“索引”出这一部分数据。虽然通过 Python 提供的索引操作符"[]“和属性操作符”."可以访问 Series 或者 DataFrame 中的数据,但这种方式只适应与少量的数据,为了解决这一问题,Pandas 提供了两种类型的索引方式来实现数据的访问。
本节就来讲解一下,如何在 Pandas 中使用 loc 函数和 iloc 函数。两种函数说明如下:
.loc[] 具有多种访问方法,如下所示:
一个标量标签
标签列表
切片对象
布尔数组
loc[] 接受两个参数,并以’,'分隔。第一个位置表示行,第二个位置表示列。示例如下:
import numpy as npimport pandas as pd#创建一组数据data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']df = pd.DataFrame(data, index=label)print(df)#对行操作print(df.loc['a':'d',:]) #等同于df.loc['a':'d']
输出结果:
name age gender isMarrieda John 20.0 0 yesb Mike 32.0 0 yesc Mozla 29.0 1 nod Rose NaN 1 yese David 15.0 0 nof Marry 28.0 1 nog Wansi 21.0 0 noh Sidy 30.0 0 yesi Jack 37.0 1 noj Alic 25.0 1 no#从a到d,切记包含dname age gender isMarrieda John 20.0 0 yesb Mike 32.0 0 yesc Mozla 29.0 1 nod Rose NaN 1 yes
对列进行操作,示例如下:
import numpy as npimport pandas as pd#创建一组数据data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']df = pd.DataFrame(data, index=label)print(df.loc[:,'name'])
输出结果:
aJohnbMikec MozladRosee Davidf Marryg WansihSidyiJackjAlicName: name, dtype: object
对行和列同时操作,示例如下:
import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(8, 4),index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])print(df.loc[['a','b','f','h'],['A','C']])
输出如下:
A Ca 1.168658 0.008070b -0.076196 0.455495f 1.224038 1.234725h 0.050292 -0.031327布尔值操作,示例如下:import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(4, 4),index = ['a','b','c','d'], columns = ['A', 'B', 'C', 'D'])#返回一组布尔值print(df.loc['b']>0)
输出结果:
ATrueBTrueC FalseDTrueName: b, dtype: bool.iloc[]
df.iloc[] 只能使用整数索引,不能使用标签索引,通过整数索引切片选择数据时,前闭后开(不包含边界结束值)。同 Python 和 NumPy 一样,它们的索引都是从 0 开始。
.iloc[] 提供了以下方式来选择数据:
整数索引
整数列表
数值范围
示例如下:
data = {'name': ['John', 'Mike', 'Mozla', 'Rose', 'David', 'Marry', 'Wansi', 'Sidy', 'Jack', 'Alic'],'age': [20, 32, 29, np.nan, 15, 28, 21, 30, 37, 25],'gender': [0, 0, 1, 1, 0, 1, 0, 0, 1, 1],'isMarried': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}label = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']df = pd.DataFrame(data, index=label)print(df)print(df.iloc[2:,])
输出结果:
name age gender isMarrieda John 20.0 0 yesb Mike 32.0 0 yesc Mozla 29.0 1 nod Rose NaN 1 yese David 15.0 0 nof Marry 28.0 1 nog Wansi 21.0 0 noh Sidy 30.0 0 yesi Jack 37.0 1 noj Alic 25.0 1 noname Mozlaage 29gender 1isMarried noName: c, dtype: object
再看一组示例:
import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randn(8, 4), columns = ['A', 'B', 'C', 'D'])print df.iloc[[1, 3, 5], [1, 3]]print df.iloc[1:3, :]print df.iloc[:,1:3]
输出结果:
B D1 0.773595 -0.2060613 -1.740403 -0.4643835 1.046009 0.606808A B C D1 -0.093711 0.773595 0.966408 -0.2060612 -1.122587 -0.135011 0.546475 -0.551403B C0 0.623488 3.3284061 0.773595 0.9664082 -0.135011 0.5464753 -1.740403 -0.8690734 0.591573 -1.4632755 1.046009 2.3300356 -0.266607 0.8739717 -1.059625 -0.405340