pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
1引言
Pandas是作为Python数据分析著名的工具包,提供了多种数据选取的方法,方便实用。本文主要介绍Pandas的几种数据选取的方法。
Pandas中,数据主要保存为Dataframe和Series是数据结构,这两种数据结构数据选取的方式基本一致,本文主要以Dataframe为例进行介绍。
在Dataframe中选取数据大抵包括3中情况:
1)行(列)选取(单维度选取):df[]。这种情况一次只能选取行或者列,即一次选取中,只能为行或者列设置筛选条件(只能为一个维度设置筛选条件)。
2)区域选取(多维选取):df.loc[],df.iloc[],df.ix[]。这种方式可以同时为多个维度设置筛选条件。
3)单元格选取(点选取):df.at[],df.iat[]。准确定位一个单元格。
接下来,我们以下面的数据为例,分别通过实例介绍这三种情况。
>>>importpandasaspd >>>importnumpyasnp >>>data={'name':['Joe','Mike','Jack','Rose','David','Marry','Wansi','Sidy','Jason','Even'], 'age':[25,32,18,np.nan,15,20,41,np.nan,37,32], 'gender':[1,0,1,1,0,1,0,0,1,0], 'isMarried':['yes','yes','no','yes','no','no','no','yes','no','no']} >>>labels=['a','b','c','d','e','f','g','h','i','j'] >>>df=pd.DataFrame(data,index=labels) >>>df nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no dRoseNaN1yes eDavid15.00no fMarry20.01no gWansi41.00no hSidyNaN0yes iJason37.01no jEven32.00no
2行(列)选取:df[]
行(列)选取是在单一维度上进行数据的选取,即以行为单位进行选取或者以列为单位进行选取。Dataframe对象的行有索引(index),默认情况下是[0,1,2,……]的整数序列,也可以自定义添加另外的索引,例如上面的labels,(为区分默认索引和自定义的索引,在本文中将默认索引称为整数索引,自定义索引称为标签索引)。Dataframe对象的每一列都有列名,可以通过列名实现对列的选取。
1)选取行
选取行的方式包括三种:整数索引切片、标签索引切片和布尔数组。
a)整数索引切片:前闭后开
选取第一行:
>>>df[0:1] nameagegenderisMarried aJoe25.01yes
选取前两行:
>>>df[0:2] nameagegenderisMarried aJoe25.01yes bMike32.00yes
b)标签索引切片:前闭后闭
选取第一行:
>>>df[:'a'] nameagegenderisMarried aJoe25.01yes
选取前两行:
>>>df['a':'b'] nameagegenderisMarried aJoe25.01yes bMike32.00yes
注意:整数索引切片是前闭后开,标签索引切片是前闭后闭,这点尤其要注意。
c)布尔数组
选取前三行
>>>df[[True,True,True,False,False,False,False,False,False,False]] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no
选取所有age大于30的行
>>>df[[each>30foreachindf['age']]] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no
通过布尔数组的方式,又可以衍生出下面的选取方式:
选取所有age大于30的行
>>>df[df['age']>30] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no
选取出所有age大于30,且isMarried为no的行
>>>df[(df['age']>30)&(df['isMarried']=='no')] nameagegenderisMarried gWansi41.00no iJason37.01no jEven32.00no
选取出所有age为20或32的行
>>>df[(df['age']==20)|(df['age']==32)] nameagegenderisMarried bMike32.00yes fMarry20.01no jEven32.00no
注意:像上面这种通过多个布尔条件判断的情况,多个条件最好(一定)用括号括起来,否则非常容易出错。
2)列选取
列选取方式也有三种:标签索引、标签列表、Callable对象
a)标签索引:选取单个列
选取name列所有数据
>>>df['name'] aJoe bMike cJack dRose eDavid fMarry gWansi hSidy iJason jEven Name:name,dtype:object
b)标签列表:选取多个列
选取name和age两列数据
>>>df[['name','age']] nameage aJoe25.0 bMike32.0 cJack18.0 dRoseNaN eDavid15.0 fMarry20.0 gWansi41.0 hSidyNaN iJason37.0 jEven32.0
c)callable对象
选取第一列
>>>df[lambdadf:df.columns[0]] aJoe bMike cJack dRose eDavid fMarry gWansi hSidy iJason jEven Name:name,dtype:object
3区域选取
区域选取可以从多个维度(行和列)对数据进行筛选,可以通过df.loc[],df.iloc[],df.ix[]三种方法实现。采用df.loc[],df.iloc[],df.ix[]这三种方法进行数据选取时,方括号内必须有两个参数,第一个参数是对行的筛选条件,第二个参数是对列的筛选条件,两个参数用逗号隔开。df.loc[],df.iloc[],df.ix[]的区别如下:
df.loc[]只能使用标签索引,不能使用整数索引,通过便签索引切边进行筛选时,前闭后闭。
df.iloc[]只能使用整数索引,不能使用标签索引,通过整数索引切边进行筛选时,前闭后开。;
df.ix[]既可以使用标签索引,也可以使用整数索引。
下面分别通过实例演示这三种方法。
3.1df.loc[]
1)对行进行选取
选取索引为‘a'的行:
>>>df.loc['a',:] nameJoe age25 gender1 isMarriedyes Name:a,dtype:object
选取索引为‘a'或‘b'或‘c'的行
>>>df.loc[['a','b','c'],:] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no
选取从‘a'到‘d'的所有行(包括‘d'行)
>>>df.loc['a':'d',:] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no dRoseNaN1yes
用布尔数组选取前3行
>>>df.loc[[True,True,True,False,False,False],:] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no
选取所有age大于30的行
>>>df.loc[df['age']>30,:] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no
也可以使用下面两方法:
>>>df.loc[df.loc[:,'age']>30,:] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no >>>df.loc[df.iloc[:,1]>30,:] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no
用callable对象选取age大于30的所有行
>>>df.loc[lambdadf:df['age']>30,:] nameagegenderisMarried bMike32.00yes gWansi41.00no iJason37.01no jEven32.00no
2)对列选取
输出所有人的姓名(选取name列)
>>>df.loc[:,'name'] aJoe bMike cJack dRose eDavid fMarry gWansi hSidy iJason jEven Name:name,dtype:object
输出所有人的姓名和年龄(选取name和age列)
>>>df.loc[:,'name':'age'] nameage aJoe25.0 bMike32.0 cJack18.0 dRoseNaN eDavid15.0 fMarry20.0 gWansi41.0 hSidyNaN iJason37.0 jEven32.0
输出所有人的姓名、年龄、婚否(选取name、age、isMarried列)
>>>df.loc[:,['name','age','isMarried']] nameageisMarried aJoe25.0yes bMike32.0yes cJack18.0no dRoseNaNyes eDavid15.0no fMarry20.0no gWansi41.0no hSidyNaNyes iJason37.0no jEven32.0no
用布尔数组的方式选取前3列
>>>df.loc[:,[True,True,True,False]] nameagegender aJoe25.01 bMike32.00 cJack18.01 dRoseNaN1 eDavid15.00 fMarry20.01 gWansi41.00 hSidyNaN0 iJason37.01 jEven32.00
3)同时对行和列进行筛选
输出年龄大于30的人的姓名和年龄
>>>df.loc[df['age']>30,['name','age']] nameage bMike32.0 gWansi41.0 iJason37.0 jEven32.0
输出行名为‘Mike'或‘Marry'的姓名和年龄
>>>df.loc[(df['name']=='Mike')|(df['name']=='Marry'),['name','age']] nameage bMike32.0 fMarry20.0
3.2df.iloc[]
1)行选取
选取第2行
>>>df.iloc[1,:] nameMike age32 gender0 isMarriedyes Name:b,dtype:object
选取前3行
>>>df.iloc[:3,:] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no
选取第2行、第4行、第6行
>>>df.iloc[[1,3,5],:] nameagegenderisMarried bMike32.00yes dRoseNaN1yes fMarry20.01no
通过布尔数组选取前3行
>>>df.iloc[[True,True,True,False,False,False],:] nameagegenderisMarried aJoe25.01yes bMike32.00yes cJack18.01no
2)列选取
选取第2列
>>>df.iloc[:,1] a25.0 b32.0 c18.0 dNaN e15.0 f20.0 g41.0 hNaN i37.0 j32.0 Name:age,dtype:float64
选取前3列
>>>df.iloc[:,0:3] nameagegender aJoe25.01 bMike32.00 cJack18.01 dRoseNaN1 eDavid15.00 fMarry20.01 gWansi41.00 hSidyNaN0 iJason37.01 jEven32.00 l选取第1列、第3列、第4列
选取第1列、第3列和第4列
>>>df.iloc[:,[0,2,3]] namegenderisMarried aJoe1yes bMike0yes cJack1no dRose1yes eDavid0no fMarry1no gWansi0no hSidy0yes iJason1no jEven0no
通过布尔数组选取前3列
>>>df.iloc[:,[True,True,True,False]] nameagegender aJoe25.01 bMike32.00 cJack18.01 dRoseNaN1 eDavid15.00 fMarry20.01 gWansi41.00 hSidyNaN0 iJason37.01 jEven32.00
3)同时选取行和列
选取第2行的第1列、第3列、第4列
>>>df.iloc[1,[0,2,3]] nameMike gender0 isMarriedyes Name:b,dtype:object
选取前3行的前3列
>>>df.iloc[:3,:3] nameagegender aJoe25.01 bMike32.00 cJack18.01
3.3df.ix[]
df.ix[]既可以通过整数索引进行数据选取,也可以通过标签索引进行数据选取,换句话说,df.ix[]是df.loc[]和df.iloc[]的功能集合,且在同义词选取中,可以同时使用整数索引和标签索引。
选取第3行的name数据
>>>df.ix[2,'name'] 'Jack'
选取a行、c行的第1列,第2列和第4列数据
>>>df.ix[['a','c'],[0,1,3]] nameageisMarried aJoe25.0yes cJack18.0no
选取所有未婚者的姓名和年龄
>>>df.ix[df['isMarried']=='no',['name','age']] nameage cJack18.0 eDavid15.0 fMarry20.0 gWansi41.0 iJason37.0 jEven32.0
4单元格选取
单元格选取包括df.at[]和df.iat[]两种方法。df.at[]和df.iat[]使用时必须输入两个参数,即行索引和列索引,其中df.at[]只能使用标签索引,df.iat[]只能使用整数索引。df.at[]和df.iat[]选取的都是单个单元格(单行单列),所以返回值都为基本数据类型。
4.1df.at[]
选取b行的name列
>>>df.at['b','name'] 'Mike'
4.2df.iat[]
选取第2行第1列
>>>df.iat[1,0] 'Mike'
5拓展与总结
1)选取某一整行(多个整行)或某一整列(多个整列)数据时,可以用df[]、df.loc[]、df.iloc[],此时df[]的方法书写要简单一些。
2)进行区域选取时,如果只能用标签索引,则使用df.loc[]或df.ix[],如果只能用整数索引,则用df.iloc[]或df.ix[]。不过我看到有资料说,不建议使用df.ix[],因为df.loc[]和df.iloc[]更精确(有吗?我没理解精确在哪,望告知)。
3)如果选取单元格,则df.at[]、df.iat[]、df.loc[]、df.iloc[]都可以,不过要注意参数。
4)选取数据时,返回值存在以下情况:
- 如果返回值包括单行多列或多行单列时,返回值为Series对象;
- 如果返回值包括多行多列时,返回值为DataFrame对象;
- 如果返回值仅为一个单元格(单行单列)时,返回值为基本数据类型,例如str,int等。
5)df[]的方式只能选取行和列数据,不能精确到单元格,所以df[]的返回值一定DataFrame或Series对象。
6)当使用DataFrame的默认索引(整数索引)时,整数索引即为标签索引。例如,使用上面的data实例化一个DataFrame对象:
>>>df2=pd.DataFrame(data) >>>df2.loc[1,'name'] 'Mike' >>>df2.iloc[1,0] 'Mike'
到此这篇关于pandas数据选取:df[]df.loc[]df.iloc[]df.ix[]df.at[]df.iat[]的文章就介绍到这了,更多相关pandas数据选取内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!