Syntax:
Parameters:
- DataFrame_object refers to the existing DataFrame.
- Headers keep the columns in the DataFrame after converting.
- 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.
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.
Output:
Table 2: Fancy_Grid Format
Fancy_grid style is like organizing the DataFrame in a grid with neat margins.
Output:
Table 3: Plain
Plain is similar to the plain DataFrame format. No margins are created in this format.
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.
Output:
When you run this HTML code in the browser, you will see the following table:
Table 5: Github
Convert to “github” format.
Output:
Table 6: Pretty Format
The “pretty” format is same as the psql only.
Output:
Table 7: TCSV Format
If you want to see your DataFrame in the tab which is separated by CSV, you can use “tcsv”.
Output:
Table 8: CSV Format
Convert the DataFrame to csv format.
Output:
Table 9: Excel format
Convert the DataFrame to excel format. This is similar to CSV.
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”.