-
Notifications
You must be signed in to change notification settings - Fork 2
/
Untitled.py
193 lines (96 loc) · 3.9 KB
/
Untitled.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#Install Libraries
import pandas as pd
import numpy as np
# In[ ]:
#Read in IMDB Name Basics
imdb_title = pd.read_csv('imdb.name.basics.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_title
# In[ ]:
#Read in IMDB title akas
imdb_akas = pd.read_csv('imdb.title.akas.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_akas
# In[ ]:
#Read in IMDB title basics
imdb_basics = pd.read_csv('imdb.title.basics.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_basics
# In[ ]:
#Read in IMDB title crew
imdb_crew = pd.read_csv('imdb.title.crew.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_crew
# In[ ]:
#Read in IMDB title principals
imdb_principals = pd.read_csv('imdb.title.principals.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_principals
# In[ ]:
#Read in IMDB title ratings
imdb_ratings = pd.read_csv('imdb.title.ratings.csv.gz', compression='gzip', header=0, sep=',', quotechar='"')
imdb_ratings
# In[ ]:
#Merging the dataframes
imdb_rating_principal = pd.merge(imdb_principals, imdb_ratings, on='tconst', how='outer')
imdb_rating_principal_crew = pd.merge(imdb_rating_principal, imdb_crew, on='tconst', how='outer')
imdb_rating_principal_crew_basics = pd.merge(imdb_rating_principal_crew, imdb_basics, on='tconst', how='outer')
imdb_rating_principal_crew_basics_akas = pd.merge(imdb_rating_principal_crew_basics, imdb_akas,
left_on="tconst", right_on="title_id", how='outer')
imdb_rating_principal_crew_basics_akas_title = pd.merge(imdb_rating_principal_crew_basics_akas, imdb_title,
on="nconst", how='outer')
imdb_rating_principal_crew_basics_akas_title
# In[70]:
#Resetting the DF name for brevity and to preserve orignial df
df = imdb_rating_principal_crew_basics_akas_title
# In[ ]:
df.head()
# In[72]:
#DF shape view
df.shape
# In[73]:
#Viewing NaN values
df.isna().sum()
# In[74]:
#Dropping columns with high NaN values
#langauge- over 87% of the column is Nan
#attributes - over 95% of the column is Nan
#death_year - over 98% of the column is Nan
#birth_year - over 61% of the column is Nan
#The job - over 74% of the column is Nan
#characters - over 61% of the column is Nan
df.drop(["language", "attributes", "death_year","birth_year", "job", "characters"], axis =1, inplace=True)
#directors and writers columns - they are a repeat of the nconstant
#title_id - a repeat of the tconstant
df.drop(["title_id", "directors", "writers"], axis =1, inplace=True)
# # Question 1: What is the correlation between runtime & genre?
# In[75]:
#Creating a new df to isolate the genre/ runtime data
runtime_genres = df.drop(["nconst", "tconst", "ordering_x", "category", "averagerating", "numvotes", "primary_title",
"original_title", "start_year", "ordering_y", "title", "region", "types",
"is_original_title", "primary_name", "primary_profession", "known_for_titles"], axis =1)
# In[76]:
runtime_genres.shape
# In[83]:
runtime_genres.isna().sum()
# In[78]:
#Drop the genre rows that have nan values - with more time you could look at the occurance of each genre and then replace
#it with a random genre
runtime_genres.dropna(subset= ['genres'], inplace= True)
# In[80]:
#Calculating Runtime Mean
runtime_genres['runtime_minutes'].mean()
#97.96914147492264
# In[81]:
#Calculating Runtime Median
runtime_genres['runtime_minutes'].median()
#95.0
# In[ ]:
#Replacing NaN values with Runtime Mean based on calculations ^
median=95
runtime_genres.fillna(median, inplace=True)
# In[ ]:
#Calculation Correlation
runtime_genres.apply(lambda x: x.factorize()[0]).corr()
# In[ ]:
#Visualizing the correlation
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()