I blogged about the functions I wrote to print tables in the IPython notebook. Today a realized: pandas already has this functionality, actually it has everything I need for my data-analysis-tasks and a lot more. Here is how I print a sqlite table using pandas. It works with any kind of "table" data-structure.
In [6]:
import sys
sys.path.append("/Users/ganwell/Documents/Programming/edubs-import.src/import/lib/")
from adsy.ipython import *
import sqlite3
import pandas
import sys
In [7]:
db = sqlite3.connect("/Users/ganwell/Downloads/nw.db")
nbconvert will print part of the table outside the window. This fixes that.
In [8]:
HTML("""
<style type="text/css">
table.nowrap {
margin-right: 80px;
}
table.dataframe {
margin-right: 80px;
}
</style>""")
Out [8]:
Standard printformat for pandas is already good enough.
In [9]:
c = db.execute("""
SELECT * FROM regions
""")
df = pandas.DataFrame(list(c), columns=[x[0] for x in c.description])
df
Out [9]:
Sometimes I need to see all data:
In [10]:
pandas.set_printoptions(max_colwidth=-1)
HTML(df.to_html())
Out [10]:
Here is what my printers look like.
In [13]:
c = db.execute("""
SELECT * FROM regions
""")
print_html(c)
Out [13]:
BTW display_html from adsy.display, display panda DataFrames with same format used for cursors and multidicts. Just call dh(my_dataframe).
ReplyDeletehttps://github.com/adfinis-sygroup/adsy-python/blob/master/display.py
I just released a project called ipy_table to provide an easy mechanism for creating richly formatted data tables in IPython notebooks (colors, borders, alignment, float formatting, etc.). The project is at https://github.com/epmoyer/ipy_table, and you can get a good idea of it's capabilities from https://github.com/epmoyer/ipy_table/blob/master/ipy_table-Reference.pdf.
ReplyDeleteGreat! Awesome. I have to integrate cursors, multi-dicts and pandas DataFrames into ipy_table!
Delete