Formatted display of pandas DataFrame

Sample data: Rakuten recipe category

from tabulate import tabulate
import pandas as pd
from pprint import pprint

df = pd.read_csv('small_category.csv', encoding='utf-8-sig')
br = '\n'

print(br + 'print'.center(20, '=') + br)
print(df.head())

print(br + 'pprint'.center(20, '=') + br)
pprint(df.head())

print(br + 'tabulate psql'.center(20, '=') + br)
print(tabulate(df.head(), headers='keys',
               tablefmt='psql',
               numalign='right',
               stralign='left', showindex=False))

print(br + 'tabulate simple'.center(20, '=') + br)
print(tabulate(df.head(), headers='keys',
               tablefmt='simple',
               numalign='right',
               stralign='left', showindex=False))

----------output----------

=======print========

     Id   Type  mediumId  largeId         Name
0 50 small 66 10 Sausage wiener
1 1491 small 67 10 Prosciutto
2 1492 small 67 10 chicken ham
3 321 small 67 10 Other hams
4 49 small 68 10 bacon

=======pprint=======

     Id   Type  mediumId  largeId         Name
0 50 small 66 10 Sausage wiener
1 1491 small 67 10 Prosciutto
2 1492 small 67 10 chicken ham
3 321 small 67 10 Other hams
4 49 small 68 10 bacon

===tabulate psql====

+------+--------+------------+-----------+-------------+
|   Id | Type   |   mediumId |   largeId | Name        |
|------+--------+------------+-----------+-------------|
|   50 | small  |         66 |        10 |Sausage wiener|
| 1491 | small  |         67 |        10 |Raw ham|
| 1492 | small  |         67 |        10 |chicken ham|
|  321 | small  |         67 |        10 |Other ham|
|   49 | small  |         68 |        10 |bacon|
+------+--------+------------+-----------+-------------+

==tabulate simple===

  Id  Type      mediumId    largeId  Name
----  ------  ----------  ---------  -----------
50 small 66 10 sausage wiener
1491 small 67 10 Prosciutto
1492 small 67 10 chicken ham
321 small 67 10 Other ham
49 small 68 10 bacon



Recommended Posts

Formatted display of pandas DataFrame
[Python] Operation memo of pandas DataFrame
[Pandas_flavor] Add a method of Pandas DataFrame
Basic operation of Python Pandas Series and Dataframe (1)
3D plot Pandas DataFrame
Basic operation of pandas
About MultiIndex of pandas
Basic operation of Pandas
Display of fractions (list)
Python application: Pandas # 3: Dataframe
Summary of pre-processing practices for Python beginners (Pandas dataframe)
[Python] Summary of table creation method using DataFrame (pandas)
Japanese display of matplotlib, seaborn
Basic usage of Pandas Summary
Behavior of pandas rolling () method
Index of certain pandas usage
Export pandas dataframe to excel
The Power of Pandas: Python
How to find the memory address of a Pandas dataframe value
DataFrame of pandas From creating a DataFrame from two lists to writing a file
Python hand play (Pandas / DataFrame beginning)
Pandas / DataFrame Tips for practical use
[Pandas] Save DataFrame as JSON, load JSON as DataFrame
Create a pandas Dataframe from a string.
Features of pd.NA in pandas 1.0.0 (rc0)
Etosetra related to read_csv of Pandas
Waveform display of audio in Python
[Memo] Small story of pandas, numpy
Bulk Insert Pandas DataFrame with psycopg2
Separate display of Python graphs (memo)
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display