探索电影数据集
在这个项目中,使用 NumPy
、Pandas
、matplotlib
、seaborn
库中的函数,来对电影数据集进行探索。
下载数据集:
TMDb电影数据
数据集各列名称的含义:
列名称 | id | imdb_id | popularity | budget | revenue | original_title | cast | homepage | director | tagline | keywords | overview | runtime | genres | production_companies | release_date | vote_count | vote_average | release_year | budget_adj | revenue_adj |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
含义 | 编号 | IMDB 编号 | 知名度 | 预算 | 票房 | 名称 | 主演 | 网站 | 导演 | 宣传词 | 关键词 | 简介 | 时常 | 类别 | 发行公司 | 发行日期 | 投票总数 | 投票均值 | 发行年份 | 预算(调整后) | 票房(调整后) |
第一节 数据的导入与处理
任务1.1: 导入库以及数据
- 载入需要的库
NumPy
、Pandas
、matplotlib
、seaborn
。 - 利用
Pandas
库,读取tmdb-movies.csv
中的数据,保存为movie_data
。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import time
%matplotlib inline
%timeit
movie_data = pd.read_csv('C:/Users/Administrator/Documents/Explore_Movie_Dataset/Explore Movie Dataset/tmdb-movies.csv')
**任务1.2: ** 了解数据
movie_data.head(2)
id | imdb_id | popularity | budget | revenue | original_title | cast | homepage | director | tagline | ... | overview | runtime | genres | production_companies | release_date | vote_count | vote_average | release_year | budget_adj | revenue_adj | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Action|Adventure|Science Fiction|Thriller | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 |
1 | 76341 | tt1392190 | 28.419936 | 150000000 | 378436354 | Mad Max: Fury Road | Tom Hardy|Charlize Theron|Hugh Keays-Byrne|Nic... | http://www.madmaxmovie.com/ | George Miller | What a Lovely Day. | ... | An apocalyptic story set in the furthest reach... | 120 | Action|Adventure|Science Fiction|Thriller | Village Roadshow Pictures|Kennedy Miller Produ... | 5/13/15 | 6185 | 7.1 | 2015 | 1.379999e+08 | 3.481613e+08 |
2 rows × 21 columns
movie_data.dtypes
id int64
imdb_id object
popularity float64
budget int64
revenue int64
original_title object
cast object
homepage object
director object
tagline object
keywords object
overview object
runtime int64
genres object
production_companies object
release_date object
vote_count int64
vote_average float64
release_year int64
budget_adj float64
revenue_adj float64
dtype: object
movie_data.isnull().sum()
id 0
imdb_id 10
popularity 0
budget 0
revenue 0
original_title 0
cast 76
homepage 7930
director 44
tagline 2824
keywords 1493
overview 4
runtime 0
genres 23
production_companies 1030
release_date 0
vote_count 0
vote_average 0
release_year 0
budget_adj 0
revenue_adj 0
dtype: int64
movie_data.shape
(10866, 21)
movie_data.describe()
id | popularity | budget | revenue | runtime | vote_count | vote_average | release_year | budget_adj | revenue_adj | |
---|---|---|---|---|---|---|---|---|---|---|
count | 10866.000000 | 10866.000000 | 1.086600e+04 | 1.086600e+04 | 10866.000000 | 10866.000000 | 10866.000000 | 10866.000000 | 1.086600e+04 | 1.086600e+04 |
mean | 66064.177434 | 0.646441 | 1.462570e+07 | 3.982332e+07 | 102.070863 | 217.389748 | 5.974922 | 2001.322658 | 1.755104e+07 | 5.136436e+07 |
std | 92130.136561 | 1.000185 | 3.091321e+07 | 1.170035e+08 | 31.381405 | 575.619058 | 0.935142 | 12.812941 | 3.430616e+07 | 1.446325e+08 |
min | 5.000000 | 0.000065 | 0.000000e+00 | 0.000000e+00 | 0.000000 | 10.000000 | 1.500000 | 1960.000000 | 0.000000e+00 | 0.000000e+00 |
25% | 10596.250000 | 0.207583 | 0.000000e+00 | 0.000000e+00 | 90.000000 | 17.000000 | 5.400000 | 1995.000000 | 0.000000e+00 | 0.000000e+00 |
50% | 20669.000000 | 0.383856 | 0.000000e+00 | 0.000000e+00 | 99.000000 | 38.000000 | 6.000000 | 2006.000000 | 0.000000e+00 | 0.000000e+00 |
75% | 75610.000000 | 0.713817 | 1.500000e+07 | 2.400000e+07 | 111.000000 | 145.750000 | 6.600000 | 2011.000000 | 2.085325e+07 | 3.369710e+07 |
max | 417859.000000 | 32.985763 | 4.250000e+08 | 2.781506e+09 | 900.000000 | 9767.000000 | 9.200000 | 2015.000000 | 4.250000e+08 | 2.827124e+09 |
**任务1.3: ** 清理数据
在真实的工作场景中,数据处理往往是最为费时费力的环节。但是幸运的是,我们提供给大家的 tmdb 数据集非常的「干净」,不需要大家做特别多的数据清洗以及处理工作。在这一步中,你的核心的工作主要是对数据表中的空值进行处理。你可以使用 .fillna()
来填补空值,当然也可以使用 .dropna()
来丢弃数据表中包含空值的某些行或者列。
任务:使用适当的方法来清理空值,并将得到的数据保存。
movie_data_nonul = movie_data.dropna(axis=1).copy()
第二节 根据指定要求读取数据
相比 Excel 等数据分析软件,Pandas 的一大特长在于,能够轻松地基于复杂的逻辑选择合适的数据。因此,如何根据指定的要求,从数据表当获取适当的数据,是使用 Pandas 中非常重要的技能,也是本节重点考察大家的内容。
**任务2.1: ** 简单读取
- 读取数据表中名为
id
、popularity
、budget
、runtime
、vote_average
列的数据。 - 读取数据表中前1~20行以及48、49行的数据。
- 读取数据表中第50~60行的
popularity
那一列的数据。
要求:每一个语句只能用一行代码实现。
movie_data_nonul.id.head()
movie_data_nonul['popularity']
movie_data_nonul['vote_average'].head()
0 6.5
1 7.1
2 6.3
3 7.5
4 7.3
Name: vote_average, dtype: float64
movie_data_nonul[:20]
movie_data_nonul[48:50]
id | popularity | budget | revenue | original_title | runtime | release_date | vote_count | vote_average | release_year | budget_adj | revenue_adj | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
48 | 265208 | 2.932340 | 30000000 | 0 | Wild Card | 92 | 1/14/15 | 481 | 5.3 | 2015 | 2.759999e+07 | 0.000000e+00 |
49 | 254320 | 2.885126 | 4000000 | 9064511 | The Lobster | 118 | 10/8/15 | 638 | 6.6 | 2015 | 3.679998e+06 | 8.339346e+06 |
movie_data_nonul[50:61]['popularity']
50 2.883233
51 2.814802
52 2.798017
53 2.793297
54 2.614499
55 2.584264
56 2.578919
57 2.575711
58 2.557859
59 2.550747
60 2.487849
Name: popularity, dtype: float64
**任务2.2: **逻辑读取(Logical Indexing)
- 读取数据表中
popularity
大于5 的所有数据。 - 读取数据表中
popularity
大于5 的所有数据且发行年份在1996年之后的所有数据。
提示:Pandas 中的逻辑运算符如 &
、|
,分别代表且
以及或
。
要求:请使用 Logical Indexing实现。
movie_data_nonul[movie_data_nonul['popularity'] > 5]
id | popularity | budget | revenue | original_title | runtime | release_date | vote_count | vote_average | release_year | budget_adj | revenue_adj | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 135397 | 32.985763 | 150000000 | 1513528810 | Jurassic World | 124 | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 |
1 | 76341 | 28.419936 | 150000000 | 378436354 | Mad Max: Fury Road | 120 | 5/13/15 | 6185 | 7.1 | 2015 | 1.379999e+08 | 3.481613e+08 |
2 | 262500 | 13.112507 | 110000000 | 295238201 | Insurgent | 119 | 3/18/15 | 2480 | 6.3 | 2015 | 1.012000e+08 | 2.716190e+08 |
3 | 140607 | 11.173104 | 200000000 | 2068178225 | Star Wars: The Force Awakens | 136 | 12/15/15 | 5292 | 7.5 | 2015 | 1.839999e+08 | 1.902723e+09 |
4 | 168259 | 9.335014 | 190000000 | 1506249360 | Furious 7 | 137 | 4/1/15 | 2947 | 7.3 | 2015 | 1.747999e+08 | 1.385749e+09 |
5 | 281957 | 9.110700 | 135000000 | 532950503 | The Revenant | 156 | 12/25/15 | 3929 | 7.2 | 2015 | 1.241999e+08 | 4.903142e+08 |
6 | 87101 | 8.654359 | 155000000 | 440603537 | Terminator Genisys | 125 | 6/23/15 | 2598 | 5.8 | 2015 | 1.425999e+08 | 4.053551e+08 |
7 | 286217 | 7.667400 | 108000000 | 595380321 | The Martian | 141 | 9/30/15 | 4572 | 7.6 | 2015 | 9.935996e+07 | 5.477497e+08 |
8 | 211672 | 7.404165 | 74000000 | 1156730962 | Minions | 91 | 6/17/15 | 2893 | 6.5 | 2015 | 6.807997e+07 | 1.064192e+09 |
9 | 150540 | 6.326804 | 175000000 | 853708609 | Inside Out | 94 | 6/9/15 | 3935 | 8.0 | 2015 | 1.609999e+08 | 7.854116e+08 |
10 | 206647 | 6.200282 | 245000000 | 880674609 | Spectre | 148 | 10/26/15 | 3254 | 6.2 | 2015 | 2.253999e+08 | 8.102203e+08 |
11 | 76757 | 6.189369 | 176000003 | 183987723 | Jupiter Ascending | 124 | 2/4/15 | 1937 | 5.2 | 2015 | 1.619199e+08 | 1.692686e+08 |
12 | 264660 | 6.118847 | 15000000 | 36869414 | Ex Machina | 108 | 1/21/15 | 2854 | 7.6 | 2015 | 1.379999e+07 | 3.391985e+07 |
13 | 257344 | 5.984995 | 88000000 | 243637091 | Pixels | 105 | 7/16/15 | 1575 | 5.8 | 2015 | 8.095996e+07 | 2.241460e+08 |
14 | 99861 | 5.944927 | 280000000 | 1405035767 | Avengers: Age of Ultron | 141 | 4/22/15 | 4304 | 7.4 | 2015 | 2.575999e+08 | 1.292632e+09 |
15 | 273248 | 5.898400 | 44000000 | 155760117 | The Hateful Eight | 167 | 12/25/15 | 2389 | 7.4 | 2015 | 4.047998e+07 | 1.432992e+08 |
16 | 260346 | 5.749758 | 48000000 | 325771424 | Taken 3 | 109 | 1/1/15 | 1578 | 6.1 | 2015 | 4.415998e+07 | 2.997096e+08 |
17 | 102899 | 5.573184 | 130000000 | 518602163 | Ant-Man | 115 | 7/14/15 | 3779 | 7.0 | 2015 | 1.195999e+08 | 4.771138e+08 |
18 | 150689 | 5.556818 | 95000000 | 542351353 | Cinderella | 112 | 3/12/15 | 1495 | 6.8 | 2015 | 8.739996e+07 | 4.989630e+08 |
19 | 131634 | 5.476958 | 160000000 | 650523427 | The Hunger Games: Mockingjay - Part 2 | 136 | 11/18/15 | 2380 | 6.5 | 2015 | 1.471999e+08 | 5.984813e+08 |
20 | 158852 | 5.462138 | 190000000 | 209035668 | Tomorrowland | 130 | 5/19/15 | 1899 | 6.2 | 2015 | 1.747999e+08 | 1.923127e+08 |
21 | 307081 | 5.337064 | 30000000 | 91709827 | Southpaw | 123 | 6/15/15 | 1386 | 7.3 | 2015 | 2.759999e+07 | 8.437300e+07 |
629 | 157336 | 24.949134 | 165000000 | 621752480 | Interstellar | 169 | 11/5/14 | 6498 | 8.0 | 2014 | 1.519800e+08 | 5.726906e+08 |
630 | 118340 | 14.311205 | 170000000 | 773312399 | Guardians of the Galaxy | 121 | 7/30/14 | 5612 | 7.9 | 2014 | 1.565855e+08 | 7.122911e+08 |
631 | 100402 | 12.971027 | 170000000 | 714766572 | Captain America: The Winter Soldier | 136 | 3/20/14 | 3848 | 7.6 | 2014 | 1.565855e+08 | 6.583651e+08 |
632 | 245891 | 11.422751 | 20000000 | 78739897 | John Wick | 101 | 10/22/14 | 2712 | 7.0 | 2014 | 1.842182e+07 | 7.252661e+07 |
633 | 131631 | 10.739009 | 125000000 | 752100229 | The Hunger Games: Mockingjay - Part 1 | 123 | 11/18/14 | 3590 | 6.6 | 2014 | 1.151364e+08 | 6.927528e+08 |
634 | 122917 | 10.174599 | 250000000 | 955119788 | The Hobbit: The Battle of the Five Armies | 144 | 12/10/14 | 3110 | 7.1 | 2014 | 2.302728e+08 | 8.797523e+08 |
635 | 177572 | 8.691294 | 165000000 | 652105443 | Big |