![6e30c90878d351bb88a3a96b5dc33448.png](https://img-blog.csdnimg.cn/img_convert/6e30c90878d351bb88a3a96b5dc33448.png)
一、索引
索引/选择的基本语法如下:
![f6a6f149cfea153d9cf912d22a318938.png](https://img-blog.csdnimg.cn/img_convert/f6a6f149cfea153d9cf912d22a318938.png)
import numpy as np
import pandas as pd
df = pd.read_csv('pandas/joyful-pandas-master/data/table.csv',index_col='ID')
df.head()
![e811813fe9ba261824b220a48c102f5e.png](https://img-blog.csdnimg.cn/img_convert/e811813fe9ba261824b220a48c102f5e.png)
一、单级索引
1. loc方法、iloc方法、[]操作符
最常用的索引方法可能就是这三类,其中iloc表示位置索引,loc表示标签索引,[]也具有很大的便利性,各有特点
基于标签的索引
- .loc 是基于标签的索引,必须使用数据的标签属性,否则会返回一个异常。
- 基于标签索引的基本语法 : df.loc[行索引,列索引]
- 逗号之前是切取的行信息,逗号之后是切取的列信息
- 只切取行信息返回所有列信息的时候,列索引和逗号可以省略
- 只切取列信息返回所有行信息的时候,行索引和冒号不能省略,用冒号表示切取所有行.
(a)loc方法(注意:所有在loc中使用的切片全部包含右端点!)
① 单行索引:
df.loc[1103]
![4d6a9627f12ba54061db2f6b14c01c4b.png](https://img-blog.csdnimg.cn/img_convert/4d6a9627f12ba54061db2f6b14c01c4b.png)
② 多行索引:
df.loc[[1102,2304]]
![29b9611b2df017c09612ef087c608ae0.png](https://img-blog.csdnimg.cn/img_convert/29b9611b2df017c09612ef087c608ae0.png)
df.loc[1304:].head()
![fd8190e283be4e7c52e43cbc4628d806.png](https://img-blog.csdnimg.cn/img_convert/fd8190e283be4e7c52e43cbc4628d806.png)
df.loc[2402::-1].head()
![22231766bc0f63e04c472c9bcccaae24.png](https://img-blog.csdnimg.cn/img_convert/22231766bc0f63e04c472c9bcccaae24.png)
③ 单列索引:
df.loc[:,'Height'].head()
![5b258dca4a3e388de0611cda65719ac9.png](https://img-blog.csdnimg.cn/img_convert/5b258dca4a3e388de0611cda65719ac9.png)
④ 多列索引:
df.loc[:,['Height','Math']].head()
![a7e8ee49b7ea9099894b8ae4590e90cb.png](https://img-blog.csdnimg.cn/img_convert/a7e8ee49b7ea9099894b8ae4590e90cb.png)
df.loc[:,'Height':'Math'].head()
# 注意:切片的时候,如果是基于标签(行名或列名)的切片,指定切哪里就能切出哪里,如果是基于位置的,切片内容包含起始位置不包含结束位置
![99008a1569518506d8ddf4ad177c0b00.png](https://img-blog.csdnimg.cn/img_convert/99008a1569518506d8ddf4ad177c0b00.png)
⑤ 联合索引:
df.loc[1102:2401:3,'Height':'Math'].head()
![12666ac6a7584653b38fede9ade23f02.png](https://img-blog.csdnimg.cn/img_convert/12666ac6a7584653b38fede9ade23f02.png)
⑥ 函数式索引:
df.loc[lambda x:x['Gender']=='M'].head()
# loc中使用的函数,传入参数就是前面的df
![87ec0f79acb574b07f07364f0bb4e536.png](https://img-blog.csdnimg.cn/img_convert/87ec0f79acb574b07f07364f0bb4e536.png)
def f(x):
return [1101,1103]
df.loc[f]
![7fe63eb244995545ed7502360a0a8fc7.png](https://img-blog.csdnimg.cn/img_convert/7fe63eb244995545ed7502360a0a8fc7.png)
⑦ 布尔索引(将重点在第2节介绍)
df.loc[df['Address'].isin(['street_7','street_4'])].head()
![2f71999d2134c443f2d3a7e7b201a589.png](https://img-blog.csdnimg.cn/img_convert/2f71999d2134c443f2d3a7e7b201a589.png)
df.loc[[True if i[-1]=='4' or i[-1]=='7' else False for i in df['Address'].values]].head()
![2378502ba11665846de41f9feb169306.png](https://img-blog.csdnimg.cn/img_convert/2378502ba11665846de41f9feb169306.png)
小节:本质上说,loc中能传入的只有布尔列表和索引子集构成的列表,只要把握这个原则就很容易理解上面那些操作.
(b)iloc方法(注意与loc不同,切片右端点不包含)
基于位置的索引
- .iloc 是基于位置的索引,传入的参数为目标区域的位置索引。
- 基于位置索引的基本语法 : df.iloc[行索引,列索引]
- 逗号之前是切取的行信息,逗号之后是切取的列信息
- 只切取行信息返回所有列信息的时候,列索引和逗号可以省略
- 只切取列信息返回所有行信息的时候,行索引和冒号不能省略,用冒号表示切取所有行.
直接切取一行信息的时候
- df["行信息":"行信息"] 返回是dataframe
- (基于标签的索引)df.loc["行信息"] 返回的是series
① 单行索引:
df.iloc[3]
![cad2dcaacec1d3bf3939afdccd90a988.png](https://img-blog.csdnimg.cn/img_convert/cad2dcaacec1d3bf3939afdccd90a988.png)
② 多行索引:
df.iloc[3:5]
![0bba368f0890901571e9310e321820af.png](https://img-blog.csdnimg.cn/img_convert/0bba368f0890901571e9310e321820af.png)
③ 单列索引:
df.iloc[:,3].head()
![27456e9bb46204595b09f5ef653ace24.png](https://img-blog.csdnimg.cn/img_convert/27456e9bb46204595b09f5ef653ace24.png)
④ 多列索引:
df.iloc[:,7::-2].head()
![483e7c39c534fb5457ae1d300abb6cd5.png](https://img-blog.csdnimg.cn/img_convert/483e7c39c534fb5457ae1d300abb6cd5.png)
⑤ 混合索引:
df.iloc[3::4,7::-2].head()
![ff14e2aa44425554b62e3a0aebf11ca4.png](https://img-blog.csdnimg.cn/img_convert/ff14e2aa44425554b62e3a0aebf11ca4.png)
⑥ 函数式索引:
df.iloc[lambda x:[3]].head()
![27d5dab432ddff139b64dace6b10c2ec.png](https://img-blog.csdnimg.cn/img_convert/27d5dab432ddff139b64dace6b10c2ec.png)
小节:由上所述,iloc中接收的参数只能为整数或整数列表,不能使用布尔索引
(c) []操作符
如果不想陷入困境,请不要在行索引为浮点时使用[]操作符,因为在Series中的浮点[]并不是进行位置比较,而是值比较,非常特殊
(c.1)Series的[]操作
① 单元素索引:
s = pd.Series(df['Math'],index=df.index)
s[1101]
#使用的是索引标签
34.0
② 多行索引:
s[0:4]
# 使用的是绝对位置的整数切片,与元素无关,这里容易混淆
![207a8db45c3ad91559da101a78c4390d.png](https://img-blog.csdnimg.cn/img_convert/207a8db45c3ad91559da101a78c4390d.png)
③ 函数式索引:
s[lambda x: x.index[16::-6]]
#注意使用lambda函数时,直接切片(如:s[lambda x: 16::-6])就报错,此时使用的不是绝对位置切片,而是元素切片,非常易错
![3c08356d328f3456d1ab676255e9077e.png](https://img-blog.csdnimg.cn/img_convert/3c08356d328f3456d1ab676255e9077e.png)
④ 布尔索引:
s[s>80]