0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Markdownで、DataFrameを表として表示させる方法

Last updated at Posted at 2020-05-23

以下の関数を実行することで、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 | 

それをコピペしてMarkdownファイルに出力すると、
以下のように表として表示することができる。

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?