Skip to content

Instantly share code, notes, and snippets.

@kovid-rathee
Created January 25, 2017 19:05
Show Gist options
  • Save kovid-rathee/aedf35e7a1d2dea5db56a0f8b44e1f7d to your computer and use it in GitHub Desktop.
Save kovid-rathee/aedf35e7a1d2dea5db56a0f8b44e1f7d to your computer and use it in GitHub Desktop.
Script to connect to a MySQL source, fetch data and print loosely formatted data using pandas DataFrame
import MySQLdb as mdb
import pandas as pd
con = mdb.connect(‘127.0.0.1’, ‘root’, ‘password’, ‘database_name’);
with con:
cur = con.cursor()
cur.execute(“select random_number_one, random_number_two, random_number_three from randomness.a_random_table”)
rows = cur.fetchall()
df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: ‘Random Number One’, 1: ‘Random Number Two’, 2: ‘Random Number Three’}, inplace=True);
print(df.head(20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment