city = college_data["CITY"]print(city)print("<"+"="*75+">")print("類型爲:",type(city))
INSTNM
Alabama A & M University Normal
University of Alabama at Birmingham Birmingham
Amridge University Montgomery
University of Alabama in Huntsville Huntsville
...
Rasmussen College - Overland Park Overland Park
National Personal Training Institute of Cleveland Highland Heights
Bay Area Medical Academy - San Jose Satellite Location San Jose
Excel Learning Center-San Antonio South San Antonio
Name: CITY, Length: 7535, dtype: object
<===========================================================================>
類型爲: <class 'pandas.core.series.Series'>
INSTNM
Alabama A & M University Normal
University of Alabama at Birmingham Birmingham
Amridge University Montgomery
University of Alabama in Huntsville Huntsville
Name: CITY, dtype: object
14.1.2.3 分片獲取
# 獲取整數索引 [0,10) 步長爲2,這樣選出的依然爲Series
city[0:10:2]
INSTNM
Alabama A & M University Normal
Amridge University Montgomery
Alabama State University Montgomery
Central Alabama Community College Alexander City
Auburn University at Montgomery Montgomery
Name: CITY, dtype: object
14.1.3 loc 用法
傳入索引標籤選取一個
city["Alabama A & M University"]
'Normal'
14.1.3.1 通過標籤列表選取多行
# 這樣通過傳入的標籤索引列表選取多行,返回的依然是Series
city[["Alabama A & M University","Amridge University"]]
INSTNM
Alabama A & M University Normal
Amridge University Montgomery
Name: CITY, dtype: object
14.1.3.2 分片選取
# 選取標籤索引 [start_target,end_target] 步長爲1的行,返回的是Series,注意這裏是端點值都能取到
city["Alabama A & M University":"University of Alabama in Huntsville":1]
INSTNM
Alabama A & M University Normal
University of Alabama at Birmingham Birmingham
Amridge University Montgomery
University of Alabama in Huntsville Huntsville
Name: CITY, dtype: object
14.2 選取DataFrame的行
14.2.1 iloc用法
14.2.1.1 傳入一個整數索引值獲取一行數據(返回類型爲Series)
college_data.iloc[0]
CITY Normal
STABBR AL
HBCU 1
MENONLY 0
...
PCTFLOAN 0.8284
UG25ABV 0.1049
MD_EARN_WNE_P10 30300
GRAD_DEBT_MDN_SUPP 33888
Name: Alabama A & M University, Length: 26, dtype: object
# 獲取標籤索引對應的數據行,返回類型爲Series
college_data.loc["University of Alabama at Birmingham"]
CITY Birmingham
STABBR AL
HBCU 0
MENONLY 0
...
PCTFLOAN 0.5214
UG25ABV 0.2422
MD_EARN_WNE_P10 39700
GRAD_DEBT_MDN_SUPP 21941.5
Name: University of Alabama at Birmingham, Length: 26, dtype: object
14.2.2.2 傳入一個標籤列表獲取多行
# 根據傳入的標籤列表返回相應的數據行,返回類型爲DataFrame
college_data.loc[["University of Alabama at Birmingham","The University of Alabama"]]
CITY
STABBR
HBCU
MENONLY
...
PCTFLOAN
UG25ABV
MD_EARN_WNE_P10
GRAD_DEBT_MDN_SUPP
INSTNM
University of Alabama at Birmingham
Birmingham
AL
0.0
0.0
...
0.5214
0.2422
39700
21941.5
The University of Alabama
Tuscaloosa
AL
0.0
0.0
...
0.4010
0.0853
41900
23750
2 rows × 26 columns
14.2.2.3 分片獲取
# 獲取[start_target,end_tartget] 步長爲 1 的數據行,返回爲DataFrame
college_data.loc["University of Alabama at Birmingham":"University of Alabama in Huntsville":1]
CITY
STABBR
HBCU
MENONLY
...
PCTFLOAN
UG25ABV
MD_EARN_WNE_P10
GRAD_DEBT_MDN_SUPP
INSTNM
University of Alabama at Birmingham
Birmingham
AL
0.0
0.0
...
0.5214
0.2422
39700
21941.5
Amridge University
Montgomery
AL
0.0
0.0
...
0.7795
0.8540
40100
23370
University of Alabama in Huntsville
Huntsville
AL
0.0
0.0
...
0.4596
0.2640
45500
24097
3 rows × 26 columns
14.3 同時選取DataFrame的行和列
14.3.1 獲取前n行m列
14.3.1.1 用 iloc 方法實現
# 獲取前面兩行三列數據
college_data.iloc[:2,:3]
CITY
STABBR
HBCU
INSTNM
Alabama A & M University
Normal
AL
1.0
University of Alabama at Birmingham
Birmingham
AL
0.0
14.3.1.2 用 loc方法 實現
# 獲取行索引從[start_target,end_target]的行,和列索引爲[start,end]的列
college_data.loc[:"University of Alabama at Birmingham",:"HBCU"]
CITY
STABBR
HBCU
INSTNM
Alabama A & M University
Normal
AL
1.0
University of Alabama at Birmingham
Birmingham
AL
0.0
14.3.2 獲取全部行中的前n列
14.3.2.1 用 iloc 方法實現
college_data.iloc[:,:2]
CITY
STABBR
INSTNM
Alabama A & M University
Normal
AL
University of Alabama at Birmingham
Birmingham
AL
Amridge University
Montgomery
AL
University of Alabama in Huntsville
Huntsville
AL
...
...
...
Rasmussen College - Overland Park
Overland Park
KS
National Personal Training Institute of Cleveland
Highland Heights
OH
Bay Area Medical Academy - San Jose Satellite Location
San Jose
CA
Excel Learning Center-San Antonio South
San Antonio
TX
7535 rows × 2 columns
14.3.2.2 用 loc 方法實現
college_data.loc[:,:"STABBR"]
CITY
STABBR
INSTNM
Alabama A & M University
Normal
AL
University of Alabama at Birmingham
Birmingham
AL
Amridge University
Montgomery
AL
University of Alabama in Huntsville
Huntsville
AL
...
...
...
Rasmussen College - Overland Park
Overland Park
KS
National Personal Training Institute of Cleveland
Highland Heights
OH
Bay Area Medical Academy - San Jose Satellite Location
San Jose
CA
Excel Learning Center-San Antonio South
San Antonio
TX
7535 rows × 2 columns
14.3.3 選取不連續的行和列
14.3.3.1 用 iloc 方法實現
college_data.iloc[[1,3,5,7],[2,4,6,8]]
HBCU
WOMENONLY
SATVRMID
DISTANCEONLY
INSTNM
University of Alabama at Birmingham
0.0
0.0
570.0
0.0
University of Alabama in Huntsville
0.0
0.0
595.0
0.0
The University of Alabama
0.0
0.0
555.0
0.0
Athens State University
0.0
0.0
NaN
0.0
14.3.3.2 用 loc 方法實現
# 這裏實現的需求同上
college_data.loc[["University of Alabama at Birmingham","University of Alabama in Huntsville","The University of Alabama","Athens State University"],["HBCU","WOMENONLY","SATVRMID","DISTANCEONLY"]]
HBCU
WOMENONLY
SATVRMID
DISTANCEONLY
INSTNM
University of Alabama at Birmingham
0.0
0.0
570.0
0.0
University of Alabama in Huntsville
0.0
0.0
595.0
0.0
The University of Alabama
0.0
0.0
555.0
0.0
Athens State University
0.0
0.0
NaN
0.0
14.3.4 選取某一個標量的值
14.3.4.1 用 iloc方法實現
# 選取第四行四列的值
college_data.iloc[3,3]
0.0
14.3.4.2 用 loc 方法實現
# 實現的需求同上
college_data.loc["Athens State University","MENONLY"]