Python

pandas.DataFrameのprintで列や行を省略せずに表示する方法

Share this for your friends.

pandas.DataFrameのprintで列や行を省略せずに表示する方法

pandasのデータフレームをprintしたら何か省略されるんだけど…


pandas.DataFrame は表形式のデータを扱うのに便利である。

CSVやEXCELのデータを取り込んで、プログラムで集計加工ができる。

しかしときには処理を作りながら処理途中のデータを見たいことがある。

print(df)

そんなときはこのように print を使うのだが、データの量や項目数が多い場合、表示しきれずに省略されてしまう

確認したい内容が含まれていればよいが、省略された方に確認したい内容が含まれていると処理内容が正しいのか間違っているのか確認できずにとても困る

列や行を省略せずに表示するにはどうしたら良いのか。

そこで今回はpandas.DataFrameのprintで列や行を省略せずに表示する方法について紹介する。

この記事を書いている人


システムエンジニア、AIエンジニアと、IT業界で10年以上働いている中堅。PythonとSQLが得意。

記事を読むメリット

pandas.DataFrameのprintで列や行を省略せずに表示する方法がわかり、pandasを使ったプログラムのデバッグに困らなくなる


データ準備

DataFrameにするデータをまず用意する。

今回は意図的に省略を起こすので、20列80行くらいのCSVデータを用意し、以下の内容のCSVを pandas_print_all.csv として保存する。

CSV

col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20


そしてpandas.DataFrameとして read_csv() で読み込み、 print してみる。

DATA

import pandas as pd

df1 = pd.read_csv("pandas_print_all.csv")
print(df1)

#     col1  col2  col3  col4  col5  ...  col16  col17  col18  col19  col20
# 0      1     2     3     4     5  ...     16     17     18     19     20
# 1      1     2     3     4     5  ...     16     17     18     19     20
# 2      1     2     3     4     5  ...     16     17     18     19     20
# 3      1     2     3     4     5  ...     16     17     18     19     20
# 4      1     2     3     4     5  ...     16     17     18     19     20
# ..   ...   ...   ...   ...   ...  ...    ...    ...    ...    ...    ...
# 74     1     2     3     4     5  ...     16     17     18     19     20
# 75     1     2     3     4     5  ...     16     17     18     19     20
# 76     1     2     3     4     5  ...     16     17     18     19     20
# 77     1     2     3     4     5  ...     16     17     18     19     20
# 78     1     2     3     4     5  ...     16     17     18     19     20

# [79 rows x 20 columns]


行も列も5つ表示して省略された。

このデータを使って解説する。



pandas.DataFrameのprintで列や行を省略せずに表示する方法

まずpandas.DataFrameのprintできる行数や列数を確認するには pandas.options.display.max_rowspandas.options.display.max_columns を確認する。

SAMPLE

print("pd.options.display.max_rows")
print(pd.options.display.max_rows)

print("pd.options.display.max_columns")
print(pd.options.display.max_columns)

# pd.options.display.max_rows
# 60
# pd.options.display.max_columns
# 0


pandas.options.display.max_rows60 行。

pandas.options.display.max_columns0 となっているが、これは設定された画面幅に収まる範囲で表示という意味である。


そしてpandas.DataFrameのprintで列や行を省略せずに表示するには、pandas.set_option を用いる。

SAMPLE

pd.set_option('display.max_rows', 500)

print(df1)

#     col1  col2  col3  col4  col5  ...  col16  col17  col18  col19  col20
# 0      1     2     3     4     5  ...     16     17     18     19     20
# 実際は表示されているがブログが長くなるので中略
# 64     1     2     3     4     5  ...     16     17     18     19     20
# 65     1     2     3     4     5  ...     16     17     18     19     20
# 66     1     2     3     4     5  ...     16     17     18     19     20
# 67     1     2     3     4     5  ...     16     17     18     19     20
# 68     1     2     3     4     5  ...     16     17     18     19     20
# 69     1     2     3     4     5  ...     16     17     18     19     20
# 70     1     2     3     4     5  ...     16     17     18     19     20
# 71     1     2     3     4     5  ...     16     17     18     19     20
# 72     1     2     3     4     5  ...     16     17     18     19     20
# 73     1     2     3     4     5  ...     16     17     18     19     20
# 74     1     2     3     4     5  ...     16     17     18     19     20
# 75     1     2     3     4     5  ...     16     17     18     19     20
# 76     1     2     3     4     5  ...     16     17     18     19     20
# 77     1     2     3     4     5  ...     16     17     18     19     20
# 78     1     2     3     4     5  ...     16     17     18     19     20


SAMPLE

pd.set_option('display.max_rows', 500)

print(df1)

#     col1  col2  col3  col4  col5  ...  col16  col17  col18  col19  col20
# 0      1     2     3     4     5  ...     16     17     18     19     20
# 実際は表示されているがブログが長くなるので中略
# 64     1     2     3     4     5  ...     16     17     18     19     20
# 65     1     2     3     4     5  ...     16     17     18     19     20
# 66     1     2     3     4     5  ...     16     17     18     19     20
# 67     1     2     3     4     5  ...     16     17     18     19     20
# 68     1     2     3     4     5  ...     16     17     18     19     20
# 69     1     2     3     4     5  ...     16     17     18     19     20
# 70     1     2     3     4     5  ...     16     17     18     19     20
# 71     1     2     3     4     5  ...     16     17     18     19     20
# 72     1     2     3     4     5  ...     16     17     18     19     20
# 73     1     2     3     4     5  ...     16     17     18     19     20
# 74     1     2     3     4     5  ...     16     17     18     19     20
# 75     1     2     3     4     5  ...     16     17     18     19     20
# 76     1     2     3     4     5  ...     16     17     18     19     20
# 77     1     2     3     4     5  ...     16     17     18     19     20
# 78     1     2     3     4     5  ...     16     17     18     19     20


このように pandas.set_option を使うことで表示する行数や列数を変更できた。



まとめ

今回はpandas.DataFrameのprintで列や行を省略せずに表示する方法について紹介した。

ポイントは以下の通り

ココがポイント

  • 表示できる行や列の数を確認するには pd.options.display.max_columnspd.options.display.max_rows をprintすると確認できる
  • 列や行を省略せずに表示するには pd.set_optiondisplay.max_rowsdisplay.max_columns を変更する




他にもpandas関連の記事もあるので、もし気になるものがあれば見てみて欲しい


Share this for your friends.

ITipsと同じようなブログを作る方法

ブログに興味がありますか?

もしブログに興味がある場合は↓このページ↓を参考にすれば、ITipsと同じ構成でブログを作ることができます

サーバー、ドメイン、ASPと【ブログに必要なものは全て】このページに書きました。
同じ構成でブログ作るのはいいけど、記事はマネしないでネ (TДT;)

ランキング参加中

にほんブログ村 IT技術ブログへ

他にもブログやSNSで紹介してくれると励みになります。

はてブのコメントで酷評されると泣きます(´;ω;`)

-Python
-,

© 2024 ITips