Coder Perfect

Print a whole Pandas Series / DataFrame in a pretty way.


On the terminal, I do a lot of work with Series and DataFrames. A truncated sample is returned by the default __repr__ for a Series, with some head and tail values but none of the remainder.

Is there a builtin way to pretty-print the entire Series / DataFrame? Ideally, it would support proper alignment, perhaps borders between columns, and maybe even color-coding for the different columns.

Asked by Dun Peal

Solution #1

Option context can also be used with one or more options:

with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also

The choices will be automatically reset to their former values.

If you’re using jupyter-notebook, you can use show(df) instead of print(df) to use the sophisticated display logic of jupyter (like so).

Answered by tsvikas

Solution #2

There’s no need to tamper with the settings. There is a straightforward solution:


Answered by Andrey Shokhin

Solution #3

Yes, if this occurs frequently, create a function similar to this one. You may also set it to load automatically whenever you start IPython:

def print_full(x):
    pd.set_option('display.max_rows', len(x))

In terms of color, I think going overboard with colors is detrimental, although I agree that something like bootstrap’s.table-striped would be useful. You may always open a support ticket to request this feature.

Answered by Dan Allan

Solution #4

Set the following parameters for displaying full dataframes after importing pandas instead of utilizing the context manager:

pd.set_option('display.max_columns', None)  # or 1000
pd.set_option('display.max_rows', None)  # or 1000
pd.set_option('display.max_colwidth', None)  # or 199

See: for a complete list of relevant choices.


Answered by lucidyan

Solution #5

Use the tabulate package to create a table:

pip install tabulate

Consider the following usage scenario:

import pandas as pd
from io import StringIO
from tabulate import tabulate

c = """Chromosome Start End
chr1 3 6
chr1 5 7
chr1 8 9"""

df = pd.read_table(StringIO(c), sep="\s+", header=0)

print(tabulate(df, headers='keys', tablefmt='psql'))

|    | Chromosome   |   Start |   End |
|  0 | chr1         |       3 |     6 |
|  1 | chr1         |       5 |     7 |
|  2 | chr1         |       8 |     9 |

Answered by The Unfun Cat

Post is based on