Pretty Printing a pandas dataframe

257,919

Solution 1

I've just found a great tool for that need, it is called tabulate.

It prints tabular data and works with DataFrame.

from tabulate import tabulate
import pandas as pd

df = pd.DataFrame({'col_two' : [0.0001, 1e-005 , 1e-006, 1e-007],
                   'column_3' : ['ABCD', 'ABCD', 'long string', 'ABCD']})

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

+----+-----------+-------------+
|    |   col_two | column_3    |
|----+-----------+-------------|
|  0 |    0.0001 | ABCD        |
|  1 |    1e-05  | ABCD        |
|  2 |    1e-06  | long string |
|  3 |    1e-07  | ABCD        |
+----+-----------+-------------+

Note:

To suppress row indices for all types of data, pass showindex="never" or showindex=False.

Solution 2

pandas >= 1.0

If you want an inbuilt function to dump your data into some github markdown, you now have one. Take a look at to_markdown:

df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]}, index=['a', 'a', 'b'])  
print(df.to_markdown()) 

|    |   A |   B |
|:---|----:|----:|
| a  |   1 |   1 |
| a  |   2 |   2 |
| b  |   3 |   3 |

Here's what that looks like on github:

enter image description here

Note that you will still need to have the tabulate package installed.

Solution 3

A simple approach is to output as html, which pandas does out of the box:

df.to_html('temp.html')

Solution 4

If you are in Jupyter notebook, you could run the following code to interactively display the dataframe in a well formatted table.

This answer builds on the to_html('temp.html') answer above, but instead of creating a file displays the well formatted table directly in the notebook:

from IPython.display import display, HTML

display(HTML(df.to_html()))

Credit for this code due to example at: Show DataFrame as table in iPython Notebook

Solution 5

You can use prettytable to render the table as text. The trick is to convert the data_frame to an in-memory csv file and have prettytable read it. Here's the code:

from StringIO import StringIO
import prettytable    

output = StringIO()
data_frame.to_csv(output)
output.seek(0)
pt = prettytable.from_csv(output)
print pt
Share:
257,919

Related videos on Youtube

Ofer
Author by

Ofer

Updated on July 08, 2022

Comments

  • Ofer
    Ofer almost 2 years

    How can I print a pandas dataframe as a nice text-based table, like the following?

    +------------+---------+-------------+
    | column_one | col_two |   column_3  |
    +------------+---------+-------------+
    |          0 |  0.0001 | ABCD        |
    |          1 |  1e-005 | ABCD        |
    |          2 |  1e-006 | long string |
    |          3 |  1e-007 | ABCD        |
    +------------+---------+-------------+
    
  • WAF
    WAF over 9 years
    What version of pandas was this?
  • edesz
    edesz about 9 years
    Hi, the format_for_print() function does not seem to be printing the index of the Pandas DataFrame. I set the index using df.index.name = 'index' but this does not print the index column with a name.
  • Pedro M Duarte
    Pedro M Duarte over 8 years
    If you do not have access to the bleeding edge, you can do tabulate([list(row) for row in df.values], headers=list(df.columns)) to get rid of the index
  • dmn
    dmn over 7 years
    AFAIK, prettytable is largely considered abandonware. Shame, too, as it was a nice package. :(
  • Siddharth
    Siddharth over 7 years
    Doesn't work very well when you have hierarchies in row index and columns.
  • muon
    muon over 6 years
    @dmn so it's not maintained anymore?
  • Dror
    Dror over 6 years
    Make sure you do print(tabulate(df, **kwargs)) and not simply tabulate(df, **kwargs); the latter will show all new lines \n....
  • Arthur
    Arthur over 6 years
    To suppress the left index column one may want to also add showindex=False
  • noddy
    noddy over 5 years
    prettytable has not had a release since Apr 6, 2013. tabulate is its spiritual predecessor and has regular releases, the most recent being on Jan 24, 2019.
  • Sean Breckenridge
    Sean Breckenridge over 3 years
    I used the to_markdown to emit markdown from my script, and piped that into glow - (github) to render the markdown in the terminal with nice results. (Script here)
  • Parth
    Parth over 3 years
    DataFrame.to_string official docs: pandas.pydata.org/pandas-docs/stable/reference/api/…
  • cs95
    cs95 over 3 years
    @SeanBreckenridge link is either broken or unaccessible from public.
  • Sean Breckenridge
    Sean Breckenridge over 3 years
    Ah, thanks for the ping; was moved to a different folder. Here's a permalink
  • Edward
    Edward over 3 years
    With more arguments passed to tabulate, to_markdown actually support 20 + types of format (github.com/astanin/python-tabulate#table-format) and many other keywords.
  • BallpointBen
    BallpointBen about 3 years
    I'd really love for pandas to bundle tabulate as an optional dependency and allow df.to_tabular(*args, **kwargs)
  • Nick Crews
    Nick Crews over 2 years
    prettytable has been resurrected under maintainership of jazzband! Hurray! github.com/jazzband/prettytable