-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
120 lines (91 loc) · 2.6 KB
/
Copy pathcode.py
File metadata and controls
120 lines (91 loc) · 2.6 KB
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
from datetime import datetime
import pandas as pd
# Titel Tabelle befüllen
title_query = """
INSERT INTO Title
(show_id, title, date_added,
country_name,
release_year, description, rating, duration, genre)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
# Datum von z.b Septmeber 2016 zu Date values die gespeichert werden können
def parse_date(x):
if pd.isna(x):
return None
try:
return datetime.strptime(x, "%B %d, %Y").date()
except Exception:
return None
for _, row in df.iterrows():
cursor.execute(
title_query,
(
row["show_id"],
row["title"],
parse_date(row["date_added"]),
row["country"] if not pd.isna(row["country"]) else None,
row["release_year"] if not pd.isna(row["release_year"]) else None,
row["description"],
row["rating"] if not pd.isna(row["rating"]) else None,
row["duration"] if not pd.isna(row["duration"]) else None,
row["listed_in"] if not pd.isna(row["listed_in"]) else None,
),
)
# Director und directs Tabelle befüllen
director_id_map = {}
next_director_id = 1
director_insert = """
INSERT INTO Director (director_id, director_name)
VALUES (%s, %s)
"""
directs_insert = """
INSERT INTO Directs (director_id, show_id)
VALUES (%s, %s)
"""
for _, row in df.iterrows():
if pd.isna(row["director"]):
continue
directors = [d.strip() for d in row["director"].split(",")]
for d in directors:
if d not in director_id_map:
director_id_map[d] = next_director_id
cursor.execute(
director_insert,
(next_director_id, d)
)
next_director_id += 1
# Many-to-many relationship
cursor.execute(
directs_insert,
(director_id_map[d], row["show_id"])
)
#Actor und casts Tabelle befüllen
actor_id_map = {}
next_actor_id = 1
actor_insert = """
INSERT INTO Actor (actor_id, actor_name)
VALUES (%s, %s)
"""
casts_insert = """
INSERT INTO Casts (actor_id, show_id)
VALUES (%s, %s)
"""
for _, row in df.iterrows():
if pd.isna(row["cast"]):
continue
actors = [a.strip() for a in row["cast"].split(",")]
for a in actors:
if a not in actor_id_map:
actor_id_map[a] = next_actor_id
cursor.execute(
actor_insert,
(next_actor_id, a)
)
next_actor_id += 1
cursor.execute(
casts_insert,
(actor_id_map[a], row["show_id"])
)
conn.commit()
cursor.close()
conn.close()