Monday, 12 November 2012

Using pandas to print tables in ipython notebook

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]:
RegionID RegionDescription
0 1 Eastern is very long Region and a lot, meaning...
1 2 Westerns ...
2 3 Northern ...
3 4 Southern ...
Sometimes I need to see all data:
In [10]:
pandas.set_printoptions(max_colwidth=-1)
HTML(df.to_html())
Out [10]:
RegionID RegionDescription
0 1 Eastern is very long Region and a lot, meaning a awful lot of text. So long an thanks for the fish. DNA is the best!
1 2 Westerns
2 3 Northern
3 4 Southern
Here is what my printers look like.
In [13]:
c = db.execute("""
SELECT * FROM regions
""")
print_html(c)
Out [13]:
RegionIDRegionDescription
1Eastern is very long Region and a lot, meaning a awful lot of text. So long an thanks for the fish. DNA is the best!
2Westerns
3Northern
4Southern

3 comments:

  1. BTW display_html from adsy.display, display panda DataFrames with same format used for cursors and multidicts. Just call dh(my_dataframe).

    https://github.com/adfinis-sygroup/adsy-python/blob/master/display.py

    ReplyDelete
  2. 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.

    ReplyDelete
    Replies
    1. Great! Awesome. I have to integrate cursors, multi-dicts and pandas DataFrames into ipy_table!

      Delete