-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetflixETL.py
130 lines (84 loc) · 3.8 KB
/
NetflixETL.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
from pymongo import MongoClient
import pandas as pd
import numpy as np
conn = MongoClient('mongodb://localhost:27017/')
etl = conn['Netflix_ETL']
collection = etl['director_tv_shows']
collection2 = etl['country_cast_counts']
collection3 = etl['actor_director_stats']
data = pd.read_csv(filepath_or_buffer='data/netflix_titles.csv',
encoding='utf-8')
# region Data Kontrolleri
print(data.shape)
print(data.info())
print(data.isnull().sum())
# endregion
# region Boş değerlerimizi "Unknown" ile dolduralım
data['director'] = data['director'].fillna(value='Unkown')
data['cast'] = data['cast'].fillna(value='Unkown')
data['country'] = data['country'].fillna(value='Unkown')
data['date_added'] = data['date_added'].fillna(value='Unkown')
data['rating'] = data['rating'].fillna(value='Unkown')
data['duration'] = data['duration'].fillna(value='Unkown')
print(data.isna().sum())
# endregion
# region Hangi yönetmenin çalışmaları daha çok TV Show olarak kategorize edilmiş ve hangi yıllarda yayımlanmış?
tv_shows = data[data['type'] == 'TV Show'].copy()
tv_shows['director'] = tv_shows['director'].str.split(',').explode('director')
director_tv_show = (
tv_shows.groupby(['director', 'release_year'])
.size()
.reset_index(name='show_count')
)
total_tv_shows = (
director_tv_show.groupby('director')['show_count']
.sum()
.reset_index(name='total_shows')
.sort_values(by='total_shows', ascending=False)
)
director_tv_show = director_tv_show.merge(total_tv_shows, on='director')
filtered_data = director_tv_show[director_tv_show['director'] != 'Unkown']
sorted_data = filtered_data.sort_values(
by=['total_shows', 'release_year'], ascending=[False, True]
)
top_director_years_dict = sorted_data.to_dict(orient='records')
collection.insert_many(top_director_years_dict)
print(sorted_data.to_string())
# endregion
# region En Çok Oyuncuya Sahip Ülkeler
df = data[['cast', 'country']].copy()
df['cast'] = df['cast'].str.split(',')
df['country'] = df['country'].str.split(',')
df = df.explode('cast')
df = df.explode('country')
df = df[(df['cast'] != 'Unkown') & (df['country'] != 'Unkown')]
country_unique_cast_count = df.groupby('country')['cast'].nunique().reset_index(name='count').sort_values(by='count', ascending=False)
print(country_unique_cast_count)
# MongoDB'ye yükleme
country_unique_cast_count_dict = country_unique_cast_count.to_dict(orient='records')
collection2.insert_many(country_unique_cast_count_dict)
print(f'{len(country_unique_cast_count_dict)} records inserted into MongoDB.')
# endregion
# region Bir Oyuncunun Yıl Bazında En Çok Çalıştığı Yönetmen
movie_data = data[['director', 'release_year', 'cast']].copy()
movie_data['cast'] = movie_data['cast'].str.split(',').explode('cast')
movie_data['director'] = movie_data['director'].str.split(',').explode('director')
movie_data = movie_data[(movie_data['cast'] != 'Unkown') & (movie_data['release_year'] != 'Unkown') & (movie_data['director'] != 'Unkown')]
actor_director_stats = movie_data.groupby(['cast', 'release_year', 'director']).size().reset_index(name='count')
print(actor_director_stats.sort_values(by='cast', ascending=True).to_string())
actor_director_stats_dict = actor_director_stats.to_dict(orient='records')
collection3.insert_many(actor_director_stats_dict)
print(f'{len(actor_director_stats_dict)} records inserted into MongoDB.')
# endregion
# region Tüm datayı ekleyelim
data = data[~data.isin(['Unkown']).any(axis=1)]
data['cast'] = data['cast'].str.split(',')
data = data.explode('cast')
data['country'] = data['country'].str.split(',')
data = data.explode('country')
data['director'] = data['director'].str.split(',')
data = data.explode('director')
df = data.to_dict(orient='records')
collection4.insert_many(df)
print(f'{len(df)} records inserted into MongoDB.')
# endregion