- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display the Pandas DataFrame in table style
Pandas is a popular data analysis and manipulation library in Python. It offers powerful tools for reading, processing, and analyzing data, including the ability to store data in a DataFrame, which is a two-dimensional labeled data structure. One of the advantages of using Pandas is the ability to display DataFrame data in a table format, making it easy to visualize and understand the data. In this article, we will explore various ways to display a Pandas DataFrame in a table style.
When working with DataFrames, it's often useful to display them in a table format. In this article, we will explore several ways to display a Pandas DataFrame in a table style.
Displaying Pandas DataFrames in a table style is a common task in data analysis and reporting. In addition to the methods discussed in the previous answer, there are several other ways to display Pandas DataFrames in a table style. Let's explore some of them −
Using Tabulate
Tabulate is a Python library that provides a simple way to create tables from a variety of data sources, including Pandas DataFrames.
import pandas as pd from tabulate import tabulate df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'San Francisco', 'London']}) print(tabulate(df, headers='keys', tablefmt='psql'))
This code uses the tabulate() function to render the DataFrame as a table in the PostgreSQL format. We also specify the headers parameter to include the column names as headers.
We can Display the Pandas DataFrame in table style using different methods below
Using the print() Function
The simplest way to display a Pandas DataFrame in a table style is to use the print() function. When you use print(), Pandas automatically formats the data into a table format. For example −
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)
Output
name age gender 0 John 25 M 1 Jane 30 F 2 Bob 35 M 3 Alice 40 F
The print() function displays the DataFrame in a tabular format, with columns aligned and each row displayed one after the other.
Using the to_string() Function
Another way to display a Pandas DataFrame in a table style is to use the to_string() function. This function returns a string representation of the DataFrame in table format. You can then print the string using the print() function. For example −
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 table = df.to_string(index=False) print(table)
Output
name age gender John 25 M Jane 30 F Bob 35 M Alice 40 F
In this example, we first create a string representation of the DataFrame using the to_string() function with the index=False argument to exclude the index column. We then print the string using the print() function to display the DataFrame in a table style.
Using the IPython.display Module
If you are using Jupyter Notebook or IPython, you can use the IPython.display module to display a Pandas DataFrame in a table format. This module provides a display() function that can be used to display various types of objects, including Pandas DataFrames.
Example
import pandas as pd from IPython.display import display # 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 display(df)
Output
name age gender 0 John 25 M 1 Jane 30 F
These are just a few examples of the many ways to display Pandas DataFrames in a table style. The best method to use depends on the specific requirements of your analysis and reporting tasks.