Pandas可以对数据集进行各种有用的分析和操作。让我们先从最简单的查看数据开始。
我们将使用IMDB电影数据集来演示,数据集文件下载:IMDB-Movie-Data.csv
首先加载CSV数据集,并将电影标题Title
指定为索引。
import pandas as pd
movies_df = pd.read_csv("IMDB-Movie-Data.csv", index_col="Title")
head
打开新数据集时,通常要做的第一件事是,打印出几行数据看看,可使用.head()
方法,该方法可以传入要显示的行数。
movies_df.head(10)
输出
Rank Genre ... Revenue (Millions) Metascore
Title ...
Guardians of the Galaxy 1 Action,Adventure,Sci-Fi ... 333.13 76.0
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
Suicide Squad 5 Action,Adventure,Fantasy ... 325.02 40.0
The Great Wall 6 Action,Adventure,Fantasy ... 45.13 42.0
La La Land 7 Comedy,Drama,Music ... 151.06 93.0
Mindhorn 8 Comedy ... NaN 71.0
The Lost City of Z 9 Action,Adventure,Biography ... 8.01 78.0
Passengers 10 Adventure,Drama,Romance ... 100.01 41.0
[10 rows x 11 columns]
.head()
如果不指定参数,默认打印5行数据。
tail
要查看数据集末尾,可使用.tail()
方法,该方法可以传入要显示的行数。
movies_df.tail(2)
输出
Rank Genre ... Revenue (Millions) Metascore
Title ...
Search Party 999 Adventure,Comedy ... NaN 22.0
Nine Lives 1000 Comedy,Family,Fantasy ... 19.64 11.0
[2 rows x 11 columns]