In Python pandas, DataFrames can be used to present data in a tabular format. There are several ways to create pandas tables, allowing you to display datasets in a struc­tured and clear manner.

Web hosting
Get your site online fast without breaking the bank
  • Fast page loads while cutting costs by 60%
  • Keep your site open 24/7 with 99.99% uptime
  • Never run out of space with unlimited storage

What are the different ways to display a pandas DataFrame as a table?

You can display a pandas DataFrame as a table using several different methods. Below, we’ll take a look at how to create tables using print(), to_string(), style, to_html() and tabulate.

print() — Standard approach

The easiest method for dis­play­ing a pandas DataFrame as a table is by using the Python print() function. This will return a table-like rep­re­sen­ta­tion of a DataFrame:

import pandas as pd

# Create a sample DataFrame
data = {
    'Name': ['Anna', 'Ben', 'Charlie', 'Diana'], 
    'Age': [23, 30, 35, 29],
    'Occupation': ['Engineer', 'Teacher', 'Doctor', 'Designer']
}

df = pd.DataFrame(data)

# Display the DataFrame as a table
print(df)
python

Here, the pandas DataFrame is displayed as a simple table in the console. The output looks like this:

      Name  Age  Occupation
0     Anna   23    Engineer
1      Ben   30     Teacher
2  Charlie   35      Doctor
3    Diana   29    Designer
Note

If you’re using Jupyter Notebook, you don’t even need to use the print() function. Simply calling the DataFrame will result in it being displayed in a tabular format.

to_string() — DataFrames in full

The built-in pandas function DataFrame.to_string() converts a DataFrame into a Python string, ensuring that the DataFrame is shown in its entirety, no matter how large it is. Let’s take a look at how this option works using the example from above:

# Show the entire DataFrame as a table
print(df.to_string())
python

Here’s what the output looks like:

      Name  Age  Occupation
0     Anna   23    Engineer
1      Ben   30     Teacher
2  Charlie   35      Doctor
3    Diana   29    Designer

style — Styling ca­pa­bil­i­ties

With the style property, pandas also offers a built-in way to highlight and format DataFrames. In the following example, we’re going to highlight the maximum values of the dataset from above:

# Display the DataFrame with the maximum values highlighted
df.style.highlight_max(axis=0)
python

In a Jupyter Notebook, the DataFrame will be shown with the maximum values high­light­ed.

to_html() — Best for web frame­works

With DataFrame.to_html(), you can represent your DataFrame as an HTML table with just one function call. This makes it easy to directly embed DataFrames in web ap­pli­ca­tions. All you need to do is call the function on the DataFrame:

# Display the DataFrame as an HTML table
html_table = df.to_html()
print(html_table)
python

You’ll then receive HTML code that you can embed in websites:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Name</th>
      <th>Age</th>
      <th>Occupation</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Anna</td>
      <td>23</td>
      <td>Engineer</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Ben</td>
      <td>30</td>
      <td>Teacher</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Charlie</td>
      <td>35</td>
      <td>Doctor</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Diana</td>
      <td>29</td>
      <td>Designer</td>
    </tr>
  </tbody>
</table>
html
Note

The pandas function DataFrame.to_markdown() functions in a similar way to to_html(). Here, you’ll receive an output in Markdown format instead of in HTML code, which you can use in a variety of doc­u­men­ta­tion tools or blogs.

tabulate — Flexible for­mat­ting options

With the external Python module tabulate, you can display pandas DataFrames in different tabular formats. Some of the formats that are supported are GitHub Flavored Markdown (GFM), re­Struc­tured­Text and plain text. You can specify the format you want using the tablefmt parameter. In the example below, we’re going to display the DataFrame from above in GitHub Markdown:

from tabulate import tabulate

# Display the DataFrame as a table in GitHub Markdown format
print(tabulate(df, headers='keys', tablefmt='github'))
python

Here’s the output:

|  | Name | Age | Occupation |
| --- | --- | --- | --- |
| 0 | Anna | 23 | Engineer |
| 1 | Ben | 30 | Teacher |
| 2 | Charlie | 35 | Doctor |
| 3 | Diana | 29 | Designer |

Reviewer

Go to Main Menu