## 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/")
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