访问行数据,有两个方法:
.loc
– 使用索引名定位.iloc
– 使用索引序号定位
示例中的数据以电影名作为索引:
# 加载数据
movies_df = pd.read_csv("IMDB-Movie-Data.csv", index_col="Title")
movies_df.columns = ['rank', 'genre', 'description', 'director', 'actors', 'year', 'runtime',
'rating', 'votes', 'revenue_millions', 'metascore']
# 访问行数据
prom = movies_df.loc["Prometheus"]
prom
输出
rank 2
genre Adventure,Mystery,Sci-Fi
description Following clues to the origin of mankind, a te...
director Ridley Scott
actors Noomi Rapace, Logan Marshall-Green, Michael Fa...
year 2012
runtime 124
rating 7
votes 485820
revenue_millions 126.46
metascore 65
Name: Prometheus, dtype: object
也可以使用iloc
访问电影”Prometheus”的行,”Prometheus”索引序号是1。
prom = movies_df.iloc[1]
loc
和iloc
的操作类似于Python列表切片。例如,我们可以选择多行:
movie_subset = movies_df.loc['Prometheus':'Sing']
movie_subset = movies_df.iloc[1:4]
movie_subset
输出
rank genre ... revenue_millions metascore
Title ...
Prometheus 2 Adventure,Mystery,Sci-Fi ... 126.46 65.0
Split 3 Horror,Thriller ... 138.12 62.0
Sing 4 Animation,Comedy,Family ... 270.32 59.0
[3 rows x 11 columns]
使用.loc
和.iloc
选择多行操作,它们的一个重要区别是,.loc
结果中包含了影片Sing,.iloc
的结果中不包含索引4 (Suicide Squad)。
使用.iloc
进行切片与使用列表进行切片遵循相同的规则,不包括结束索引。