Advertisement
egracechoi

wine-python/sql

Dec 2nd, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. %matplotlib inline
  2. import pandas as pd
  3. import matplotlib
  4. import matplotlib.pyplot as plt
  5. matplotlib.style.use(['seaborn-talk', 'seaborn-ticks', 'seaborn-whitegrid'])
  6.  
  7. !curl 'http://stevenmasley.me/winemag-data_first150k.csv' -o wine.csv
  8.  
  9. !rm wine.csv.gz
  10. !gzip wine.csv
  11. !gzip -cd wine.csv.gz | head -3
  12.  
  13. wine = pd.read_csv("wine.csv.gz", encoding="utf-8", dtype="unicode")
  14. wine_1 = {
  15. 'Unnamed: 0':"ID"
  16. }
  17. wine.rename(columns=wine_1, inplace=True)
  18.  
  19. !sudo -H pip3 install -U sqlalchemy
  20. from sqlalchemy import create_engine
  21.  
  22. conn_string = 'mysql://{user}:{password}@{host}:{port}/?charset=utf8'.format(
  23. user='root',
  24. password='dwdstudent2015',
  25. host = 'localhost',
  26. port=3306,
  27. encoding='utf-8'
  28. )
  29. engine = create_engine(conn_string)
  30.  
  31. engine.execute('CREATE DATABASE IF NOT EXISTS wine')
  32. engine.execute('USE wine')
  33. engine.execute('DROP TABLE IF EXISTS wine')
  34.  
  35. wine.to_sql(name='wine3', if_exists='replace', index=False, con=engine, chunksize=1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement