Pandas dataframe is a 2-dimensional table structured data structure used to store data in rows and columns format.
You can pretty print pandas dataframe using pd.set_option(‘display.max_columns’, None) statement.
Usecase: Your dataframe may contain many columns and when you print it normally, you’ll only see few columns.
You can set this option to display all dataframe columns in a jupyter notebook.
In this tutorial, you’ll learn the different methods to pretty print the Pandas Dataframe.
If you’re in Hurry
You can use the below code snippet to pretty print the entire pandas dataframe.
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 3)
display(df)
To know more about setting the options for printing the dataframe, read further.
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to pretty print the entire dataframe or parts of the dataframe.
In addition to the pandas package, you’ll also use the tabulate package.
To print specific rows, read How To Print A Specific Row Of A Pandas Dataframe – Definitive Guide.
Table of Contents
Sample Dataframe
This is the sample dataframe used throughout the tutorial.
Dataframe Looks Like
from tabulate import tabulate
import pandas as pd
data = {"product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers",pd.NaT],
"Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
"No_Of_Units":[5,5, 10, 20, 20, 8,pd.NaT],
"Available_Quantity":[5,6,10,"Not Available","Not Available", pd.NaT,pd.NaT],
"Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]
}
df = pd.DataFrame(data)
df = df.astype({"Unit_Price": float})
df
Dataframe is printed using the df
object directly.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8 | NaT | 01/05/2021 |
6 | NaT | NaN | NaT | NaT | NaT |
Now, you’ll learn how to prettify the dataframe.
Pretty print dataframe as table
In this section, you’ll learn how to pretty print dataframe as a table using the display() method of the dataframe.
There are two methods to set the options for printing.
pd.set_options()
method – Sets the options for the entire sessionpd.option_context()
method – Sets the option temporarily for the current cell execution.
Available Options
The frequently used options are described below.
display.max_rows
– To set the maximum number of rows to be printeddisplay.max_columns
– To set the maximum number of columns to be printeddisplay.width
– To set the width of the table columnsdisplay.colheader_justify
– To set the alignment of the column headers.
Now let’s see how to set these options for the pandas.
Set Options Using pd.set_options()
Use the below snippet to set the properties for pretty printing the dataframe and display the dataframe using display(df)
.
Snippet
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
pd.set_option('display.colheader_justify', 'center')
pd.set_option('display.precision', 3)
display(df)
Since the max rows and max columns are set to None
, all the columns and rows of the dataframe will be printed. The column headers will be aligned to center
.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8 | NaT | 01/05/2021 |
6 | NaT | NaN | NaT | NaT | NaT |
This is how you can set the options permanently using the set_options()
.
Next, you’ll learn how to set options only for the statement context.
Set Options Using pd.option_context()
The method option_context()
in the pandas allows you to set the options to the current statement context.
The below settings will be applied only to the current statement context and only the current print()
or the display()
will be controlled by using the set options.
Snippet
with pd.option_context('display.max_rows', 5,
'display.max_columns', None,
'display.width', 1000,
'display.precision', 3,
'display.colheader_justify', 'center'):
display(df)
Where,
'display.max_rows', 5
– used to set the maximum number of rows to 5. hence only the 5 rows from the dataframe will be printed'display.max_columns', None
– Used to denote that all columns must be printed'display.width', 1000
– Used to set the width of the columns'display.precision', 3
-Used to set the precision for columns'display.colheader_justify', 'center'
– Used to set the alignment of the column headers.
Only five rows of the dataframe will be printed in a pretty format.
Dataframe Looks Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.0 | 5 | 5 | 11/5/2021 |
1 | Mouse | 200.0 | 5 | 6 | 4/23/2021 |
… | … | … | … | … | … |
5 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |
6 | NaT | NaN | NaT | NaT | NaT |
7 rows × 5 columns
This is how you can set the options temporarily to the current statement context using the option_context()
method.
Next, you’ll print the dataframe using the print statement.
Using Print Method
You can use the print()
method to print the dataframe in a table format. You can convert the dataframe to String
using the to_string()
method and pass it to the print method which will print the dataframe.
If you want to ignore the index column while printing the dataframe, you can use the parameter, index=False
as shown below.
Snippet
print(df.to_string(index=False))
The dataframe is printed without an index using the print()
method.
Output
product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
Keyboard 500.000 5 5 11/5/2021
Mouse 200.000 5 6 4/23/2021
Monitor 5000.235 10 10 08/21/2021
CPU 10000.550 20 Not Available 09/18/2021
CPU 10000.550 20 Not Available 09/18/2021
Speakers 250.500 8 NaT 01/05/2021
NaT NaN NaT NaT NaT
This is how you can pretty print the dataframe using the print()
method.
Next, you’ll learn about print dataframe to HTML.
Pretty Print Dataframe to HTML
In this section, you’ll learn how to pretty print dataframe to HTML. Both HTML files and also printing as HTML objects.
You can convert the dataframe to HTML by using the to_html()
method.
Pretty Printing to HTML File
Use the below snippet to print the dataframe to the temp.html
file.
Snippet
df.to_html('temp.html')
Where,
temp.html
– File name of the html. The html file will be created in the location of your python script itself. You can also pass the fully qualified file path likec:\users\temp.html
to create temp file in your desired location.
Pretty Printing as HTML Object
You can use the same to_html()
method to convert the dataframe to the HTML object.
just do not pass the filename to the to_html()
method and pass it to the print()
method.
Then it’ll be printed in the console of the Jupyter Notebook.
Snippet
print(df.to_html)
The dataframe will be printed as HTML as below.
Output
<bound method DataFrame.to_html of product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
0 Keyboard 500.000 5 5 11/5/2021
1 Mouse 200.000 5 6 4/23/2021
2 Monitor 5000.235 10 10 08/21/2021
3 CPU 10000.550 20 Not Available 09/18/2021
4 CPU 10000.550 20 Not Available 09/18/2021
5 Speakers 250.500 8 NaT 01/05/2021
6 NaT NaN NaT NaT NaT>
This is how you can print the dataframe as HTML.
Next, you’ll learn about printing the dataframe as markdown.
Pretty Print Dataframe to Markdown
In this section, you’ll learn how to pretty print dataframe to Markdown format.
Markdown is a lightweight markup language that is used to create formatted text using a plain-text editor.
You can use the to_markdown() method to available in the dataframe.
You can control the printing of the index column by using the flag index
.
index = False
– Will ignore the index in the Markdownindex = True
– Will print the index in the Markdown Use the below snippet to print the dataframe as a markdown without the index column.
Snippet
print(df.to_markdown(index=False))
The dataframe is printed as markdown without the index column.
Output
| product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date |
|:---------------|-------------:|:--------------|:---------------------|:-----------------------|
| Keyboard | 500 | 5 | 5 | 11/5/2021 |
| Mouse | 200 | 5 | 6 | 4/23/2021 |
| Monitor | 5000.23 | 10 | 10 | 08/21/2021 |
| CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| Speakers | 250.5 | 8 | NaT | 01/05/2021 |
| NaT | nan | NaT | NaT | NaT |
To create a markdown file from the dataframe, use the below snippet.
print(df.to_markdown('temp.md', index=False))
Where,
temp.md
– File name of the markdown. The markdown file will be created in the location of your python script itself. You can also pass the fully qualified file path likec:\users\temp.md
to create temp file in your desired location.index=False
– To ignore the index column in the markdown.
This is how you can convert the dataframe to the markdown format.
Next, you’ll learn about pretty printing the dataframe using the tabulate package.
Using Tabulate Package
In this section, you’ll learn how to use the tabulate package to pretty print the dataframe. It is a python library and also a command-line utility.
You can tabulate the dataframe and pass it to print()
method to print it.
Installing Tabulate
Use the below snippet to install tabulate in your python environment.
Snippet
pip install tabulate
Tabulate package will be installed.
Options
The most commonly used options in tabulate are given below.
headers
– Used to pass the header values for the table to be printed.tablefmt
– used to specify the format on which the table needs to be printed.
Printing as psql format
You can use the tabulate method as shown below to print it in PSQL format.
Snippet
print(tabulate(df, headers='keys', tablefmt='psql'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headrstablefmt='psql'
– To denote the dataframe needs to be printed aspsql
format.
The dataframe will be printed in PSQL format with dataframe keys as headers.
Output
+----+----------------+--------------+---------------+----------------------+------------------------+
| | product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date |
|----+----------------+--------------+---------------+----------------------+------------------------|
| 0 | Keyboard | 500 | 5 | 5 | 11/5/2021 |
| 1 | Mouse | 200 | 5 | 6 | 4/23/2021 |
| 2 | Monitor | 5000.23 | 10 | 10 | 08/21/2021 |
| 3 | CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| 4 | CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| 5 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |
| 6 | NaT | nan | NaT | NaT | NaT |
+----+----------------+--------------+---------------+----------------------+------------------------+
Printing as Fancy Grid format
You can print the dataframe using tabulate package in a fancy_grid
format. Fancy_grid is a javascript library to print the data with a number of different features.
Use the below snippet to print the data in a fancy grid format.
Snippet
print(tabulate(df, headers='keys', tablefmt='fancy_grid'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='fancy_grid'
– To denote the dataframe needs to be printed asfancy grid
format. Dataframe will be printed in a fancy grid format.
Output
╒════╤════════════════╤══════════════╤═══════════════╤══════════════════════╤════════════════════════╕
│ │ product_name │ Unit_Price │ No_Of_Units │ Available_Quantity │ Available_Since_Date │
╞════╪════════════════╪══════════════╪═══════════════╪══════════════════════╪════════════════════════╡
│ 0 │ Keyboard │ 500 │ 5 │ 5 │ 11/5/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 1 │ Mouse │ 200 │ 5 │ 6 │ 4/23/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 2 │ Monitor │ 5000.23 │ 10 │ 10 │ 08/21/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 3 │ CPU │ 10000.5 │ 20 │ Not Available │ 09/18/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 4 │ CPU │ 10000.5 │ 20 │ Not Available │ 09/18/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 5 │ Speakers │ 250.5 │ 8 │ NaT │ 01/05/2021 │
├────┼────────────────┼──────────────┼───────────────┼──────────────────────┼────────────────────────┤
│ 6 │ NaT │ nan │ NaT │ NaT │ NaT │
╘════╧════════════════╧══════════════╧═══════════════╧══════════════════════╧════════════════════════╛
Next, printing in a plain format.
Printing as Plain Format
You can print the dataframe using tabulate package in a plain
format.
The dataframe will be printed in a plain format with normal HTML tags.
Use the below snippet to print the data in a plain format.
Snippet
print(tabulate(df, headers='keys', tablefmt='plain'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='plain'
– To denote the dataframe needs to be printed asplain
format.
Dataframe will be printed in a plain html
format.
Output
product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
0 Keyboard 500 5 5 11/5/2021
1 Mouse 200 5 6 4/23/2021
2 Monitor 5000.23 10 10 08/21/2021
3 CPU 10000.5 20 Not Available 09/18/2021
4 CPU 10000.5 20 Not Available 09/18/2021
5 Speakers 250.5 8 NaT 01/05/2021
6 NaT nan NaT NaT NaT
Printing as RST Format
You can print the dataframe using tabulate package in a rest
format.
The dataframe will be printed in a restructured text format.
Use the below snippet to print the data in a rst
format.
Snippet
print(tabulate(df, headers='keys', tablefmt='rst'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='rst'
– To denote the dataframe needs to be printed as restructured text format.
Output
==== ============== ============ ============= ==================== ======================
.. product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
==== ============== ============ ============= ==================== ======================
0 Keyboard 500 5 5 11/5/2021
1 Mouse 200 5 6 4/23/2021
2 Monitor 5000.23 10 10 08/21/2021
3 CPU 10000.5 20 Not Available 09/18/2021
4 CPU 10000.5 20 Not Available 09/18/2021
5 Speakers 250.5 8 NaT 01/05/2021
6 NaT nan NaT NaT NaT
==== ============== ============ ============= ==================== ======================
Printing as HTML Format
You can print the dataframe using tabulate package in a HTML
format.
The dataframe will be printed in an HTML text format.
Use the below snippet to print the data in a html
format.
Snippet
print(tabulate(df, headers='keys', tablefmt='html'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='html'
– To denote the dataframe needs to be printed ashtml
text format.
Output
<table>
<thead>
<tr><th style="text-align: right;"> </th><th>product_name </th><th style="text-align: right;"> Unit_Price</th><th>No_Of_Units </th><th>Available_Quantity </th><th>Available_Since_Date </th></tr>
</thead>
<tbody>
<tr><td style="text-align: right;"> 0</td><td>Keyboard </td><td style="text-align: right;"> 500 </td><td>5 </td><td>5 </td><td>11/5/2021 </td></tr>
<tr><td style="text-align: right;"> 1</td><td>Mouse </td><td style="text-align: right;"> 200 </td><td>5 </td><td>6 </td><td>4/23/2021 </td></tr>
<tr><td style="text-align: right;"> 2</td><td>Monitor </td><td style="text-align: right;"> 5000.23</td><td>10 </td><td>10 </td><td>08/21/2021 </td></tr>
<tr><td style="text-align: right;"> 3</td><td>CPU </td><td style="text-align: right;"> 10000.5 </td><td>20 </td><td>Not Available </td><td>09/18/2021 </td></tr>
<tr><td style="text-align: right;"> 4</td><td>CPU </td><td style="text-align: right;"> 10000.5 </td><td>20 </td><td>Not Available </td><td>09/18/2021 </td></tr>
<tr><td style="text-align: right;"> 5</td><td>Speakers </td><td style="text-align: right;"> 250.5 </td><td>8 </td><td>NaT </td><td>01/05/2021 </td></tr>
<tr><td style="text-align: right;"> 6</td><td>NaT </td><td style="text-align: right;"> nan </td><td>NaT </td><td>NaT </td><td>NaT </td></tr>
</tbody>
</table>
Printing as GITHUB Flavored Markdown Format
You can print the dataframe using tabulate package in a github
format.
The dataframe will be printed in a GITHUB flavored markdown format.
Use the below snippet to print the data in a github
format.
Snippet
print(tabulate(df, headers='keys', tablefmt='github'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headrstablefmt='github'
– To denote the dataframe needs to be printed asgithub
flavored markdown format.
Output
| | product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date |
|----|----------------|--------------|---------------|----------------------|------------------------|
| 0 | Keyboard | 500 | 5 | 5 | 11/5/2021 |
| 1 | Mouse | 200 | 5 | 6 | 4/23/2021 |
| 2 | Monitor | 5000.23 | 10 | 10 | 08/21/2021 |
| 3 | CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| 4 | CPU | 10000.5 | 20 | Not Available | 09/18/2021 |
| 5 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |
| 6 | NaT | nan | NaT | NaT | NaT |
Printing as Pretty Format
You can print the dataframe using tabulate package in a pretty
format.
The dataframe will be printed as a pretty
markdown format.
Use the below snippet to print the data in a pretty
format.
Snippet
print(tabulate(df, headers='keys', tablefmt='pretty'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='pretty'
– To denote the dataframe needs to be printed aspretty
format.
Output
+---+--------------+------------+-------------+--------------------+----------------------+
| | product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date |
+---+--------------+------------+-------------+--------------------+----------------------+
| 0 | Keyboard | 500.0 | 5 | 5 | 11/5/2021 |
| 1 | Mouse | 200.0 | 5 | 6 | 4/23/2021 |
| 2 | Monitor | 5000.235 | 10 | 10 | 08/21/2021 |
| 3 | CPU | 10000.55 | 20 | Not Available | 09/18/2021 |
| 4 | CPU | 10000.55 | 20 | Not Available | 09/18/2021 |
| 5 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |
| 6 | NaT | nan | NaT | NaT | NaT |
+---+--------------+------------+-------------+--------------------+----------------------+
Printing as TSV Format
You can print the dataframe using tabulate package in a tab-separated format.
The dataframe will be printed as a tab separated values.
Use the below snippet to print the data in a tsv
format.
Snippet
print(tabulate(df, headers='keys', tablefmt='tsv'))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headrstablefmt='tsv'
– To denote the dataframe needs to be printed as a tab separated format.
Output
product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
0 Keyboard 500 5 5 11/5/2021
1 Mouse 200 5 6 4/23/2021
2 Monitor 5000.23 10 10 08/21/2021
3 CPU 10000.5 20 Not Available 09/18/2021
4 CPU 10000.5 20 Not Available 09/18/2021
5 Speakers 250.5 8 NaT 01/05/2021
6 NaT nan NaT NaT NaT
Handling Floating Points While Printing
You can format the floating-point numbers using the tabulate package while printing using the parameter floatfmt
.
Use the below snippet to print the data and the float numbers with the 4 decimal points. The column Unit_Price is a float data type column in the sample dataframe. Hence it’ll be printed with four decimal points.
Snippet
print(tabulate(df, headers='keys', tablefmt='tsv',floatfmt=".4f"))
Where,
df
– The source dataframeheaders='keys'
– To denote that the keys of the dataframe needs to be used as table headerstablefmt='tsv'
– To denote the dataframe needs to be printed as a tab separated format.floatfmt=".4f"
– To format the floating point numbers with four decimal points.
Output
product_name Unit_Price No_Of_Units Available_Quantity Available_Since_Date
0 Keyboard 500.0000 5 5 11/5/2021
1 Mouse 200.0000 5 6 4/23/2021
2 Monitor 5000.2350 10 10 08/21/2021
3 CPU 10000.5500 20 Not Available 09/18/2021
4 CPU 10000.5500 20 Not Available 09/18/2021
5 Speakers 250.5000 8 NaT 01/05/2021
6 NaT nan NaT NaT NaT
Conclusion
To summarize, you’ve learned how to pretty print the entire dataframe in pandas.
You’ve used the pd.set_options()
and pd.option_context()
to set the options for printing the dataframe using display() and the print() method.
Additionally, you’ve also learned how to use the methods available in the tabulate
package to pretty print the dataframe.
This tutorial shows how to pretty print a dataframe in a Jupyter notebook. However, it’s applicable in other python environments too.
If you have any questions, comment below.