In [1]:
import MySQLdb
db = MySQLdb.connect(
"db.fastcamp.us",
"root",
"dkstncks",
"world",
charset='utf8',
)
cursor = db.cursor()
In [2]:
import pandas as pd
In [3]:
# SQL Perspective
SQL_QUERY = """
SELECT SUM(Population)
FROM Country;
"""
pd.read_sql(SQL_QUERY, db)
Out[3]:
In [4]:
# Pandas Perspective
SQL_QUERY = """
SELECT *
FROM Country;
"""
df = pd.read_sql(SQL_QUERY, db)
In [5]:
df["Population"].sum()
Out[5]:
In [6]:
# New Example - MAX, MIN, MEAN, ...
SQL_QUERY = """
SELECT AVG(Population)
FROM Country;
"""
pd.read_sql(SQL_QUERY, db)
Out[6]:
In [7]:
SQL_QUERY = """
SELECT *
FROM Country;
"""
df = pd.read_sql(SQL_QUERY, db)
In [9]:
df["Population"].mean()
Out[9]:
In [11]:
SQL_QUERY = """
SELECT MAX(Population)
FROM Country;
"""
pd.read_sql(SQL_QUERY, db)
Out[11]:
In [12]:
df["Population"].max()
Out[12]: