Pretty Printing a pandas dataframe
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"
orshowindex=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:
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
Related videos on Youtube
Ofer
Updated on July 08, 2022Comments
-
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 over 9 yearsWhat version of pandas was this?
-
edesz about 9 yearsHi, the
format_for_print()
function does not seem to be printing the index of the Pandas DataFrame. I set the index usingdf.index.name = 'index'
but this does not print the index column with a name. -
Pedro M Duarte over 8 yearsIf 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 over 7 yearsAFAIK,
prettytable
is largely considered abandonware. Shame, too, as it was a nice package. :( -
Siddharth over 7 yearsDoesn't work very well when you have hierarchies in row index and columns.
-
muon over 6 years@dmn so it's not maintained anymore?
-
Dror over 6 yearsMake sure you do
print(tabulate(df, **kwargs))
and not simplytabulate(df, **kwargs)
; the latter will show all new lines\n
.... -
Arthur over 6 yearsTo suppress the left index column one may want to also add
showindex=False
-
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 over 3 yearsI used the
to_markdown
to emit markdown from my script, and piped that intoglow -
(github
) to render the markdown in the terminal with nice results. (Script here) -
Parth over 3 years
DataFrame.to_string
official docs: pandas.pydata.org/pandas-docs/stable/reference/api/… -
cs95 over 3 years@SeanBreckenridge link is either broken or unaccessible from public.
-
Sean Breckenridge over 3 yearsAh, thanks for the ping; was moved to a different folder. Here's a permalink
-
Edward over 3 yearsWith 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 about 3 yearsI'd really love for
pandas
to bundletabulate
as an optional dependency and allowdf.to_tabular(*args, **kwargs)
-
Nick Crews over 2 years
prettytable
has been resurrected under maintainership of jazzband! Hurray! github.com/jazzband/prettytable