[PYTHON] How to display DataFrame as a table in Markdown

By executing the following function, it can be output in tabular format for Markdown.

def df_table(df):
    for col in df.columns:
        print('|', col, end="", sep='')
    print('|')
    
    for col in df.columns:
        print('| ---- ', end="", sep='')
    print('|')
    
    for row in range(len(df)):
        print('|', end="")
        for col in range(df.shape[1]):
            print(df.iat[row,col],'|', end="", sep='')
        print()

| sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | 
| ---- | ---- | ---- | ---- | ---- |
| 5.1 | 3.5 | 1.4 | 0.2 | setosa | 
| 4.9 | 3.0 | 1.4 | 0.2 | setosa | 
| 4.7 | 3.2 | 1.3 | 0.2 | setosa | 
| 4.6 | 3.1 | 1.5 | 0.2 | setosa | 
| 5.0 | 3.6 | 1.4 | 0.2 | setosa | 

If you copy and paste it and output it to a Markdown file, It can be displayed as a table as shown below.

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa

Recommended Posts

How to display DataFrame as a table in Markdown
How to display multiplication table in python
How to import NoteBook as a module in Jupyter (IPython)
How to print characters as a table with Python's print function
How to import NoteBook as a module in Jupyter (IPython)
How to display a specified column of files in Linux (awk)
How to suppress display error in matplotlib
How to split and save a DataFrame
How to reassign index in pandas dataframe
<Pandas> How to handle time series data in a pivot table
How to display Hello world in python
How to get a specific column name and index name in pandas DataFrame
How to install python package in local environment as a general user
How to find a specific type (str, float etc) column in a DataFrame column
How to disguise a ZIP file as a PNG file
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
How to display videos inline in Google Colab
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
How to Mock a Public function in Pytest
[Pandas] How to check duplicates and delete duplicates in a table (equivalent to deleting duplicates in Excel)
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to display multiple images of galaxies in tiles
How to save a table scraped by python to csv
I made a command to markdown the table clipboard
How to execute a command using subprocess in Python
How to reference static files in a Django project
[Linux] How to put your IP in a variable
[GCF + Python] How to upload Excel to GCS and create a new table in BigQuery
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
How to call a function
How to hack a terminal
How to develop in Python
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
I made a command to generate a table comment in Django
Put the lists together in pandas to make a DataFrame
A handy function to add a column anywhere in a Pandas DataFrame
A note on how to load a virtual environment in PyCharm
I tried "How to get a method decorated in Python"
How to use cuML SVC as a Gridsearch CV classifier
How to generate a query using the IN operator in Django
How to check if a value exists in an enum
How to display a list of installable versions with pyenv
How to get the last (last) value in a list in Python
How to register a package on PyPI (as of September 2017)
How to get a list of built-in exceptions in python
Python learning basics ~ How to output (display) a character string? ~