# credit to Stack Overflow user in the source link from tabulate import tabulate import pandas as pd df = pd.DataFrame({'col_two' : [0.0001, 1e-005 , 1e-006, 1e-007], 'column_3' : ['ABCD', 'ABCD', 'long string', 'ABCD']}) print(tabulate(df, headers='keys', tablefmt='psql')) +----+-----------+-------------+ | | col_two | column_3 | |----+-----------+-------------| | 0 | 0.0001 | ABCD | | 1 | 1e-05 | ABCD | | 2 | 1e-06 | long string | | 3 | 1e-07 | ABCD | +----+-----------+-------------+
Here is what the above code is Doing:
1. Importing the tabulate and pandas libraries
2. Creating a dataframe with two columns and four rows
3. Using the tabulate function to print the dataframe in a psql format
4. The headers=’keys’ argument tells tabulate to use the column names as headers
5. The tablefmt=’psql’ argument tells tabulate to use the psql format
The output of the code is a nicely formatted table.
Source: https://stackoverflow.com/questions/2573135/pretty-printing-a-pandas-dataframe