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 structured and clear manner.

Web hosting
The hosting your website deserves at an unbeatable price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced protection
  • Only at IONOS: up to 500 GB included

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()andtabulate`.

`print()— Standard approach

The easiest method for displaying a pandas DataFrame as a table is by using the Python print() function. This will return a table-like representation 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)Here, the pandas DataFrame is displayed as a simple table in the console. The output looks like this:
python
Name  Age    Occupation

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

<box-hinweis>
If you’re using **[Jupyter Notebook](t3://page?uid=2063)**, you don’t even need to use the `print()function. **Simply calling** the DataFrame will result in it being displayed in a tabular format.
</box-hinweis>
### `to_string()— DataFrames in full
The built-in pandas function `DataFrame.to_string()converts a **DataFrame into a [Python string](t3://page?uid=3826)**, 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:
```python
# Show the entire DataFrame as a table
print(df.to_string())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 capabilities
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:
```python
# Display the DataFrame with the maximum values highlighted
df.style.highlight_max(axis=0)In a Jupyter Notebook, the DataFrame will be shown with the maximum values highlighted.
### `to_html()— Best for web frameworks
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 applications. All you need to do is call the function on the DataFrame:
```python
# Display the DataFrame as an HTML table
html_table = df.to_html()
print(html_table)You’ll then receive HTML code that you can embed in websites:
```html
<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>
Note

The pandas function DataFrame.to_markdown()functions in a similar way toto_html(). Here, you’ll receive an output in Markdown format instead of in HTML code, which you can use in a variety of documentation tools or blogs.

tabulate — Flexible formatting 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), reStructuredText 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))Here’s the output:
python
Name Age Occupation
0 Anna 23 Engineer
1 Ben 30 Teacher
2 Charlie 35 Doctor
3 Diana 29 Designer
Was this article helpful?
Go to Main Menu