前置知識補充:
布爾索引:指的是通過傳入一個判斷條件來選擇數據的方式,稱之爲布爾索引;
普通索引:通過選擇行/列名來選擇數據的方式,稱爲普通索引;
位置索引:通過傳入具體的位置來選擇數據的方式稱爲位置索引;
切片索引:通過傳入一個位置區間來獲取數據的方式,稱爲切片索引;
準備示例數據:
# 創建一個Serise —— 傳入一個字典
d= {
'name':['qinlu','lulu','qinqin','junjun'],
'sex':['male','male','female','male'],
'age':[18,19,22,25]
}
s= pd.DataFrame(d,index = ['A1','A2','A3','A4'])
s
-- 輸出結果:
name sex age
A1 qinlu male 18
A2 lulu male 19
A3 qinqin female 22
A4 junjun male 25
方式1:布爾索引 + 普通索引選擇指定的行和列
布爾索引 + 普通索引是先對錶進行布爾索引選擇行,然後通過普通索引選擇列:
#case1:zjh-98
s[s['age']<=22] [['name','age']]
-- 輸出結果:
name age
A1 qinlu 18
A2 lulu 19
A3 qinqin 22
# 找到符合條件的行 —— 得到數據結構,其實本身也是個數據框
# 然後再在這個數據行下,選擇我們需要的列即可,因此,就完成了,對指定行、列進行選擇;
# case2:
# 在以上基礎上,只選擇某一列,數據結構就變成了Series;
s[s['age']<=22].name
-- 輸出結果:
A1 qinlu
A2 lulu
A3 qinqin
Name: name, dtype: object
# case3:
# 行、列索引放在同一個[]中,對行列同時選擇:
s.loc[s['age']<= 22,['name','age']]
-- 輸出結果:
name age
A1 qinlu 18
A2 lulu 19
A3 qinqin 22
注意需要使用loc,不能使用iloc,否則會出現以下報錯:
# 英:ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types漢:基於位置的索引只能具有[整數,整數切片(起始點爲INCLUDED,結束點爲EXCLUDED),類似整數的列表,布爾數組)類型
講完普通索引,我們再來講,使用“切片索引 + 切片索引”來選擇指定行和列的方式:
方式2:切片索引 + 切片索引選擇指定的行和列
切片索引 + 切片索引是同時傳入行、列索引的位置區間進行數據選擇。
# 選擇第1行到第3行,第1列到第2列
s.iloc[0:3,0:3]
-- 輸出:
name sex age
A1 qinlu male 18
A2 lulu male 19
A3 qinqin female 22
-- 常見錯誤1:
s.iloc[[0:3],[0:3]]
# 行列切片加上[]會出現報錯, SyntaxError: invalid syntax/無效的語法
-- 常見錯誤2:
s.loc[0:3,0:3]]
# 位置索引注意需要使用iloc,不能使用loc,否則會出現以下報錯:英:cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>; / 漢:無法使用<class'int'>的這些索引器[0]在<class'pandas.core.indexes.base.Index'>上進行切片索引
方式3:切片索引 + 普通索引選擇指定的行和列
如果是普通索引,就直接傳入行或列名,用loc方法即可;如果是切片索引,也就是傳入行或列名,用iloc方法即;如果是切片索引 + 普通索引,也就是行(列)用切片索引,列(行)用切片索引,列(行)用普通索引,看是否能夠選擇成功:
# case1:使用iloc發生報錯
s.iloc[0:3,['name','age']]
-- 輸出報錯:
# 報錯:英:IndexError: .iloc requires numeric indexers, got ['name' 'age'];漢:iloc需要數字索引器,得到['name''age']
# 原因分析:當使用行(列)名稱索引時,不能使用iloc方法;若使用iloc,必須要求行或列都嚴格使用數字或者切片進行索引;
# case2:使用loc出現報錯
s.loc[0:3,['name','age']]
-- 報錯:
#報錯:英:TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'> / 中:TypeError:無法使用<class'int'>的這些索引器[0]在<class'pandas.core.indexes.base.Index'>上進行切片索引
# 原因分析:有數字/切片的索引不能使用loc
# case3:使用ix出現報錯
s.ix[0:3,['name','age']]
# 報錯:英:AttributeError:“ DataFrame”對象沒有屬性“ ix”;/漢:AttributeError:“ DataFrame”對象沒有屬性“ ix”
# 原因分析:新版本的anconda目前已經不支持"ix"了!
結論: 新版本的Jupyter Notebook,目前已經不支持"ix"了!
方式4:位置索引 + 位置索引選擇指定行和列
# 位置索引:選擇第1,4行,第1,3列
s.iloc[[0,3],[0,2]]
-- 輸出結果:
name age
A1 qinlu 18
A4 junjun 25
# case1:索引超出
s.iloc[[1,2],[2,3]]
# 報錯:IndexError: positional indexers are out-of-bounds/漢:IndexError:位置索引器超出範圍;
# case2:s.loc[[0,3],[0,2]]
# 報錯:KeyError: "None of [Int64Index([0, 3], dtype='int64')] are in the [index]"/ 中文:KeyError:“ [Int64Index([0,3],dtype ='int64')]都不在[索引]中”
# 原因分析:當使用位置索引 + 位置索引選擇指定行和列時,需要利用iloc方法傳入行列位置
方式5:普通索引 + 普通索引選擇指定行和列
# 普通索引 + 普通索引就是通過同時傳入行和列的索引名稱進行數據選擇,需要用到loc方法;
s.loc[['A1','A2','A4'],['name','age']]
-- 輸出結果:
name age
A1 qinlu 18
A2 lulu 19
A4 junjun 25
# 報錯:s.iloc[['A1','A2','A4'],['name','age']];英文:IndexError: .iloc requires numeric indexers, got ['A1' 'A2' 'A4']
# 原因分析:當使用普通索引 + 普通索引選擇指定行和列時,不能使用iloc