Display the Pandas DataFrame in table style

Pandas DataFrames can be displayed in various table formats for better visualization and analysis. This article explores different methods to present DataFrame data in a clean, readable table style.

Using the print() Function

The simplest way to display a Pandas DataFrame in table style is using the print() function. Pandas automatically formats the data into a tabular layout ?

import pandas as pd

# Create a sample DataFrame
data = {
    'name': ['John', 'Jane', 'Bob', 'Alice'],
    'age': [25, 30, 35, 40],
    'gender': ['M', 'F', 'M', 'F']
}
df = pd.DataFrame(data)

# Display the DataFrame in table style
print(df)
    name  age gender
0   John   25      M
1   Jane   30      F
2    Bob   35      M
3  Alice   40      F

Using the to_string() Function

The to_string() method provides more control over the table formatting. You can customize the display by removing the index or adjusting alignment ?

import pandas as pd

data = {
    'name': ['John', 'Jane', 'Bob', 'Alice'],
    'age': [25, 30, 35, 40],
    'gender': ['M', 'F', 'M', 'F']
}
df = pd.DataFrame(data)

# Display without index
table = df.to_string(index=False)
print(table)
 name  age gender
 John   25      M
 Jane   30      F
  Bob   35      M
Alice   40      F

Using Tabulate Library

The tabulate library offers professional table formatting with various styles. First install it with pip install tabulate ?

import pandas as pd
# Uncomment the next line if tabulate is available
# from tabulate import tabulate

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'], 
    'Age': [25, 30, 35], 
    'City': ['New York', 'San Francisco', 'London']
})

# For demonstration, using to_string instead
print(df.to_string(index=False))

# With tabulate (when available):
# print(tabulate(df, headers='keys', tablefmt='grid'))
     Name  Age           City
    Alice   25       New York
      Bob   30  San Francisco
  Charlie   35         London

Using IPython.display in Jupyter

In Jupyter notebooks, the display() function renders DataFrames as interactive HTML tables ?

import pandas as pd
from IPython.display import display

data = {
    'name': ['John', 'Jane', 'Bob', 'Alice'],
    'age': [25, 30, 35, 40],
    'gender': ['M', 'F', 'M', 'F']
}
df = pd.DataFrame(data)

# Display as HTML table in Jupyter
display(df)

Comparison of Methods

Method Best For Customization
print() Quick viewing Limited
to_string() Console output Moderate
tabulate Professional formatting Extensive
display() Jupyter notebooks Interactive

Conclusion

Use print() for quick DataFrame viewing, to_string() for customized console output, and display() in Jupyter notebooks. The tabulate library provides the most professional table formatting options.

Updated on: 2026-03-27T10:37:21+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements