Home »
Python »
Python Programs
How to use pandas tabulate for dataframe?
Given a pandas dataframe, we have to learn how to use pandas tabulate for it?
Submitted by Pranit Sharma, on September 07, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Python tabulate
Python tabulate is a library that is used to pretty print the DataFrame or any tabular data in a more visualized manner.
To work with tabulate, we need to install tabulate library into our system, we can do this globally through our command prompt by running the following command.
Command to install Python tabulate
To install tabulate library - run the below given command:
pip install tabulate
Once the tabulate library is installed, we can import it into our IDE and start working on it. We can simply create a DataFrame and print it by using tabulate library.
Let us understand with the help of an example.
Python program to use pandas tabulate for dataframe
# Importing pandas package
import pandas as pd
# Importing methods from tabulate
from tabulate import tabulate
# Creating a dictionary
d = {
'A':['Madhya Pradesh','Rajasthan','Gujrat','Punjab'],
'B':['Bhopal','Jaipur','Gandhinagar','Chandigarh']
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display the DataFrame
print("Original DataFrame:\n",df,"\n")
# Display DataFrame in using tabulate
print("DataFrame using tabulate:\n")
print(tabulate(df, showindex=False, headers=['States','Capitals']))
Output
Consider the below example without sample input and output:
Python Pandas Programs »