Python

Pandas Tabulate

Pandas in Python can alter a Pandas DataFrame into a table with different styles. A Pandas DataFrame is executed using the “tabulate()” method. Let’s look at our examples and discuss the procedure to transform our Python DataFrame to the different styles of tables.

Syntax:

tabulate(DataFrame_object, headers='keys', tablefmt)

Parameters:

  1. DataFrame_object refers to the existing DataFrame.
  2. Headers keep the columns in the DataFrame after converting.
  3. The tablefmt takes the table style.

We will see the different styles of tables by just modifying this parameter.

First, we create a DataFrame with 2 columns and we use this DataFrame in all table styles.

Note: Make sure to run this code in your environment because we use this code in all our examples. Otherwise, you will get errors.

import pandas
from tabulate import tabulate

# Consider the DataFrame having 5 records
dataset=pandas.DataFrame({
                        'Idea':['All','water supply','electricity','drilling','electricity'],
                        'demography':['ap','gujarat','patna','indore','norway']},index=[2,3,4,5,6])


print("Actual: \n")
print(dataset)

Output:

Table 1: PSQL Format
Convert the DataFrame to psql format.

print(tabulate(dataset, headers='keys', tablefmt='psql'))

Output:

Table 2: Fancy_Grid Format
Fancy_grid style is like organizing the DataFrame in a grid with neat margins.

print(tabulate(dataset, headers='keys', tablefmt='fancy_grid'))

Output:

Table 3: Plain
Plain is similar to the plain DataFrame format. No margins are created in this format.

print(tabulate(dataset, headers='keys', tablefmt='plain'))

Output:

Table 4: HTML
The HTML code is returned as output when we specify the tablefmt as “html”. If you want to check whether the HTML code creates a table or not, run the generated HTML code in the browser.

print(tabulate(dataset, headers='keys', tablefmt='html'))

Output:

When you run this HTML code in the browser, you will see the following table:

Table 5: Github
Convert to “github” format.

print(tabulate(dataset, headers='keys', tablefmt='github'))

Output:

Table 6: Pretty Format
The “pretty” format is same as the psql only.

print(tabulate(dataset, headers='keys', tablefmt='pretty'))

Output:

Table 7: TCSV Format
If you want to see your DataFrame in the tab which is separated by CSV, you can use “tcsv”.

print(tabulate(dataset, headers='keys', tablefmt='tsv'))

Output:

Table 8: CSV Format
Convert the DataFrame to csv format.

print(tabulate(dataset, headers='keys', tablefmt='csv'))

Output:

Table 9: Excel format
Convert the DataFrame to excel format. This is similar to CSV.

print(tabulate(dataset, headers='keys', tablefmt='excel'))

Output:

Conclusion

When rendering a DataFrame into a table code, we first need to import the tabulate module. We just change the tablefmt parameter to set the table style. By seeing the 8 styles, we came to know that Pandas have such a wonderful option to display the DataFrame in different ways that meet our project requirements. Make sure that you set the headers parameter to “keys”.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain