Why not create a stylish table easily with Python?

Python is convenient

Python is convenient. Graphs can be easily created with matplotlib, and numerical calculations need only be done with numpy. Recently, Python is used for machine learning research and can even make games.

At that time, I suddenly wondered, "Can I make diagrams with Python, but can I also make tables?"

matplotlib The answer was here. Apparently matplotlib has a function to create a table. If you write it, it looks like this.

import matplotlib.pyplot as plt
import pandas as pd


if __name__ == '__main__':
    data = {
        'a': [1.0, 2.1, 3.5, '-', 2.0, 1.0, 2.1, 3.5, 4.0, 2.0, ],
        'b': [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 8.3, '-', ],
        }

    df = pd.DataFrame(data)

    fig, ax = plt.subplots(figsize=(3, 3))

    ax.axis('off')
    ax.axis('tight')

    ax.table(cellText=df.values,
             colLabels=df.columns,
             bbox=[0, 0, 1, 1],
             )

    plt.show()

pytable

It is a little troublesome to make a table, and it is not easy to change the design of the table. To change the design

import matplotlib.pyplot as plt
import pandas as pd


if __name__ == '__main__':
    data = {
        'Tokyo': [27, 23, 27, 24, 25, 23, 26],
        'Osaka': [26, 23, 27, 28, 24, 22, 27],
    }

    df = pd.DataFrame(data)

    fig, ax = plt.subplots(figsize=(3, 3))

    ax.axis('off')
    ax.axis('tight')

    tb = ax.table(cellText=df.values,
                  colLabels=df.columns,
                  bbox=[0, 0, 1, 1],
                  )

    tb[0, 0].set_facecolor('#363636')
    tb[0, 1].set_facecolor('#363636')
    tb[0, 0].set_text_props(color='w')
    tb[0, 1].set_text_props(color='w')

    plt.show()

It needs to be like this. pytable

It's a bit deliberate, but it's a hassle if you're elaborate on the design. So, based on this ** matplotlib table function, let's create a library that can easily create stylish tables **. I also wanted to try PyPI, so I also tried to do pip install.

pyTable It's a simple name, but I created it for the time being. Of course, it is already registered on PyPI, so

$ pip install pytab

It can be installed with. I really wanted to make it ** pytable **, but I compromised because I had a previous contract. (I want you to delete it from PyPI, the one that hasn't been updated for a while.)

Then, you can create a simple and stylish table by specifying the arguments as shown below.

import pytab as pt


if __name__ == '__main__':
    data = {
        'Tokyo': [27, 23, 27, 24, 25, 23, 26],
        'Osaka': [26, 23, 27, 28, 24, 22, 27],
    }

    pt.table(
        data=data,
        th_type='dark',
        table_type='striped'
    )

    pt.show()

pytable

Other detailed tables can be defined, but a simple document is summarized below, so if you are interested, please have a look. pyTable documentation

Source Github: HiroshiARAKI/pytable: pytable is the library to plot table easily. PyPI: pytab · PyPI

Afterword

As you can see from the source code, I haven't written such a big code. With this, I think that ** library ** can often be called so proudly. You just wrapped matplotlib! I can't argue with that. Lol

Well, it's still a developing library, so we plan to make it even more convenient in the future (probably). If you are interested, please use ** pyTable !! **

Recommended Posts

Why not create a stylish table easily with Python?
You can easily create a GUI with Python
Create a Python console application easily with Click
Create a directory with python
Create a virtual environment with Python!
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a word frequency counter with Python 3.4
Create a frame with transparent background with tkinter [Python]
Create a LINE BOT with Minette for Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
Create a python3 build environment with Sublime Text3
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create a Photoshop format file (.psd) with python
Create a table of contents with IPython notebook
Create a Python module
Easily beep with python
Create a Python environment
I tried to easily create a fully automatic attendance system with Selenium + Python
I tried to create a table only with Django
[Python] Create a ValueObject with a complete constructor using dataclasses
Create a python development environment with vagrant + ansible + fabric
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
[Python] How to create a 2D histogram with Matplotlib
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a fake Minecraft server in Python with Quarry
Create a company name extractor with python using JCLdic
Create a 2d CAD file ".dxf" with python [ezdxf]
Easily serverless with Python with chalice
Create a dictionary in Python
Create / search / create table with PynamoDB
Create 3d gif with python3
Create a homepage with django
Python list is not a list
Create a python numpy array
Make a fortune with Python
Create a heatmap with pyqtgraph
Easily cProfile with a decorator
[Python] Create a file & folder path specification screen with tkinter
Create a Mastodon bot with a function to automatically reply with Python
Recommendations for django, wagtail ~ Why develop a website with python ~
Create a child account for connect with Stripe in Python
Let's create a script that registers with Ideone.com in Python.
Probably the easiest way to create a pdf with Python3
Let's create a PRML diagram with Python, Numpy and matplotlib.
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple video analysis tool with python wxpython + openCV
Creating a GUI as easily as possible with python [tkinter edition]
Create a simple Python development environment with VSCode & Docker Desktop
Tornado-Let's create a Web API that easily returns JSON with JSON
Create a python machine learning model relearning mechanism with mlflow