Getting Started

Adding Style to a DataFrame

The PrettyPandas class takes advantage of the new Pandas Style API to create custom tables for your dataframes. If you have a dataframe df, this is how that might look:

from prettypandas import PrettyPandas

table = PrettyPandas(df)
_images/1@2x.png

Note

An instance of PrettyPandas is no longer the original dataframe but a presentation of the dataframe. That means you cannot use any of the standard transformations from the pandas.DataFrame class. The PrettyPandas instance can’t change the original dataframe so there’s no fear of contaminating your data.

Adding Summaries

PrettyPandas supports many different summary functions, as well the ability to apply summaries along rows, columns, or both. Summaries chain together so you can use multiple summaries without any headaches.

The builtin summary methods are:

The above functions work nicely on your table, if you wanted to add a grand total to the bottom of your table the code is simple:

PrettyPandas(df).total()
_images/total@2x.png

And if you want to mix and match summaries:

PrettyPandas(df).total().average()
_images/average@2x.png

The axis parameter specifies which numpy style axis to apply a summary on — 0 for columns, 1 for rows, and None for both.

PrettyPandas(df).total(axis=1)
_images/alt_axis@2x.png

You can even mix and match summaries applied to different axis.

Creating a Custom Summary

The summary method creates a custom summary from a function which takes an array-like structure as a list.

def count_greater_than_five(items):
    return sum(item > 5 for item in items)

PrettyPandas(df).summary(count_greater_than_five, title="> 5")
_images/custom_fn@2x.png

Formatting Numbers

Most reports use at least some units of measurement. PrettyPandas currently supports percentages, money, and a more general unit method.

The as_unit method takes a positional unit argument which indicates the string representing the unit to be used and a location argument to specify whether the unit should be a prefix or suffix to the value.

The as_currency and as_percent methods are localized to use whatever units your Python distribution thinks are best for you. If you aren’t getting the correct units use the set_locale method to specify your locale.

If you need to use a different currency, just pass it to currency='...' to change it.

The as_money method takes optional currency and location arguments which work just like the as_unit method. By default the currency is in dollars.

Note

Python 2 doesn’t support unicode literals by default. You can use unicode literals (e.g. u'€') or import the unicode literal behaviour from Python 3:

from __future__ import unicode_literals

Formatting Columns

By default the formatting methods apply to the entire dataframe. When you need to format just a few columns you can use the subset argument to specify a single column, or multiple columns.

PrettyPandas(df).as_percent(subset='A')  # Format just column A
_images/format_a@2x.png
PrettyPandas(df).as_percent(subset=['A', 'B'])  # Format columns A and B
_images/format_a_b@2x.png

Formatting Rows and Complex Formatting

Formatting rows is more complicated than formatting columns. The subset argument needs to take in a pandas.Index to specify the row.

# Format the row with row-index 3
PrettyPandas(df, precision=2).as_percent(subset=pd.IndexSlice[3,:])
_images/format_row@2x.png

For multi-index dataframes subsetting is more complicated. You will need to use multiple pandas.IndexSlice objects to get the correct rows.

The following example shows how to select rows in a multi-index:

idx = pd.IndexSlice
first_row_idx = idx[:, 1]   # select all with index like (*, 1)
second_row_idx = idx[:, 2]  # select all with index like (*, 2)

(PrettyPandas(df2)
 .as_money(subset=idx[first_row_idx, :])
 .as_percent(subset=idx[second_row_idx, :])
 )
_images/format_complex@2x.png

For more info on Pandas indexing, read Pandas Indexing and Pandas Advanced Indexing.

The Magic Function

The apply_pretty_globals() function will patch your notebook so that all tables are styled the same. This injects HTML into the notebook (which some hosts don’t allow).