<jupyter_start><jupyter_text>Connect to SQL Database<jupyter_code>import pandas as pd 
from sqlalchemy import create_engine
import plotly.graph_objects as go

# Load and read csv
csv_file = "./data/battles.csv"
got_battles = pd.read_csv(csv_file)

csv_file4 = "./data/scripts.csv"
got_scripts = pd.read_csv(csv_file4)
got_scripts_name = got_scripts[["Name"]]
got_scripts_name.columns = map(str.lower, got_scripts_name.columns)
got_scripts_name.to_csv("")

csv_file5 = "./data/ratings.csv"
got_ratings = pd.read_csv(csv_file5)
got_ratings.columns = map(str.lower, got_ratings.columns)
got_ratings_df = got_ratings.rename(columns ={
                            "season": "season",
                            "episode number": "episode_number", 
                            "number in season": "number_in_season",
                            "episode name": "episode_name",
                            "original air date":"original_air_date",
                            "us viewers (million)":"us_viewers_millions",
                            "runtime (mins)":"runtime_mins",
                            "imdb description":"imdb_description",
                            "imdb votes":"imdb_votes",
                            "imdb rating":"imdb_rating",
                            "notable death count":"notable_death_count"
                            })
got_cleaned = got_ratings_df[["season","episode_name", "director", "imdb_rating", "notable_death_count", "imdb_description"]]
got_cleaned = got_cleaned.sort_values(by="imdb_rating", ascending=False).head(10)
got_cleaned.to_csv("got_cleaned.csv", index=False)

csv_file6 = "./gabes-folder/backend/kings.csv"
kings_df = pd.read_csv(csv_file6)
kings_df.columns = map(str.lower, kings_df.columns)

csv_file7 = "./kasy-folder/backend/director.csv"
director_df = pd.read_csv(csv_file7)
director_df.columns = map(str.lower, director_df.columns)

csv_file8 = "./kasy-folder/backend/top_episodes.csv"
top_episodes_df = pd.read_csv(csv_file8)
top_episodes_df.columns = map(str.lower, top_episodes_df.columns)
got_ratings_df
# Establish connection 

connection_string = "postgres:PW@localhost:5432/Game-of-Thrones"
engine = create_engine(f'postgresql://{connection_string}')
# Print table names
engine.table_names()

# Load data
got_battles.to_sql(name="kings_df", con=engine, if_exists='append', index=False)
got_scripts_name.to_sql(name="got_scripts_name", con=engine, if_exists='append', index=False)
got_ratings_df.to_sql(name="got_ratings_df", con=engine, if_exists='append', index=False)



# Read data using Pandas
pd.read_sql_query('select * from kings_df', con=engine)
pd.read_sql_query('select * from got_scripts_name', con=engine)
pd.read_sql_query('select * from got_ratings_df', con=engine)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12266187050359713, 'fraction_numerical': 0.0057553956834532375, 'mean_word_length': 3.658291457286432, 'pattern_counts': {'":': 11, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '273423', 'license': 'MIT', 'n_tokens_mistral': 960, 'n_tokens_neox': 934, 'n_words': 144, 'path': 'Main GOT .ipynb', 'repo_name': 'gakunkel/game-of-thrones-dashboard', 'size': '2780'}