<jupyter_start><jupyter_text><jupyter_code># Imports
import pandas as pd
from sqlalchemy import create_engine
import psycopg2


df = pd.read_csv('https://raw.githubusercontent.com/JimKing100/DS-Unit-3-Sprint-2-SQL-and-Databases/master/module2-sql-for-analysis/titanic.csv')
df = df.rename(columns={"Survived": "survived", "Pclass": "pclass", "Name": "name", "Sex": "sex",
                        "Age": "age", "Siblings/Spouses Aboard": "siblings", 
                        "Parents/Children Aboard": "parents", "Fare": "fare"})
print(df.head())

host = 'salt.db.elephantsql.com'
user = 'wsatpgnz'
database = 'wsatpgnz'
password = '<PASSWORD>'
pg_conn = psycopg2.connect(database=database, user=user, password=password, host=host)
pg_cur = pg_conn.cursor()

pg_cur.execute('DROP TABLE "public"."titanic"')
pg_conn.commit()
create_titanic_table = """
CREATE TABLE titanic(
    survived SMALLINT NOT NULL,
    pclass SMALLINT NOT NULL,
    name VARCHAR (100) NOT NULL,
    sex VARCHAR (10) NOT NULL,
    age SMALLINT NOT NULL,
    siblings SMALLINT NOT NULL,
    parents SMALLINT NOT NULL,
    fare FLOAT NOT NULL
);
"""

pg_cur.execute(create_titanic_table)
pg_conn.commit()

db_string = "postgres://wsatpgnz:<PASSWORD>@<EMAIL>:5432/wsatpgnz"
engine = create_engine(db_string)

pg_conn_2 = engine.connect()

df.to_sql('titanic', pg_conn_2, index=False, if_exists='append')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.145610278372591, 'fraction_numerical': 0.013561741613133477, 'mean_word_length': 4.676113360323886, 'pattern_counts': {'":': 8, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '483414', 'license': 'MIT', 'n_tokens_mistral': 537, 'n_tokens_neox': 514, 'n_words': 103, 'path': 'module2-sql-for-analysis/insert_titanic.ipynb', 'repo_name': 'JimKing100/DS-Unit-3-Sprint-2-SQL-and-Databases', 'size': '1461'}