Joining and creating dataframe#
The database used here is SQLite. The main data science tricks will not change much depending on the database in use because they all adhere to PEP249 (commonly known as Python DB API 2). For different databases, different third-party Python libraries (such as Psycopg2 for PostgreSQL) must be installed.
Importing libraries and packages#
1# Mathematical operations and data manipulation
2import pandas as pd
3
4# Database
5import sqlite3
Set paths#
1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"
Connect#
1with sqlite3.connect(f"{assets_path}/discworld.db") as conn:
2 cursor = conn.cursor()
Create dataframe#
1cursor.execute("PRAGMA foreign_keys = 1")
2sql = """SELECT
3 user.email,
4 user.first_name,
5 user.last_name,
6 user.age,
7 user.gender,
8 quotes.quotes FROM quotes
9 JOIN user ON quotes.user_id = user.email
10"""
11rows = cursor.execute(sql)
1columns = ["Email", "First Name", "Last Name", "Age", "Gender", "Quotes"]
2data = []
3
4for row in rows:
5 data.append(row)
1df = pd.DataFrame(data, columns=columns)
2df.style.set_properties(column=["Quotes"], **{"text-align": "left"})
3df
First Name | Last Name | Age | Gender | Quotes | ||
---|---|---|---|---|---|---|
0 | swivel@lspace.org | Geoffrey | Swivel | 20 | M | There's a billion places like home. But only o... |
1 | swivel@lspace.org | Geoffrey | Swivel | 20 | M | That's quite against the spirit of witchcraft. |
2 | swivel@lspace.org | Geoffrey | Swivel | 20 | M | You bloated lying blutocat! |
3 | swivel@lspace.org | Geoffrey | Swivel | 20 | M | Wisdom is one of the few things that looks big... |
4 | swivel@lspace.org | Geoffrey | Swivel | 20 | M | Cats gravitate to kitchens like rocks gravitat... |
... | ... | ... | ... | ... | ... | ... |
995 | grodley@lspace.com | Sister | Grodley | 39 | F | Sometimes you have to trust to experience and ... |
996 | grodley@lspace.com | Sister | Grodley | 39 | F | Humanity's a nice place to visit, but you woul... |
997 | grodley@lspace.com | Sister | Grodley | 39 | F | To tell the truth, I don't know either, but th... |
998 | grodley@lspace.com | Sister | Grodley | 39 | F | But I can't do none of that stuff. That wouldn... |
999 | grodley@lspace.com | Sister | Grodley | 39 | F | Not that I don't like him, stinky as he is. I'... |
1000 rows × 6 columns
1conn.close()