English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Pandas カスタムオプション

Pandas カスタムオプション操作の例

PandasはAPIを提供して動作をカスタマイズできるため、広く使用されています。
カスタムAPIには以下の5つの関連機能があります:

get_option()set_option()reset_option()describe_option()option_context()

これらのメソッドについて一緒に学びましょう。

get_option(param)

get_optionは1つのパラメータを受け取り、以下の値を出力します:

display.max_rows

デフォルト値の数を表示します。インタープリタはこの値を読み取り、この値を表示上限として行を表示します。

 import pandas as pd
 print(pd.get_option("display.max_rows"))

実行結果:

    60

display.max_columns

デフォルト値の数を表示します。インタープリタはこの値を読み取り、この値を表示上限として行を表示します。

 import pandas as pd
 print(pd.get_option("display.max_columns"))

実行結果:

    20

ここでは、60と20はデフォルトの設定パラメータ値です。

set_option(param,value)

set_optionは2つの引数を受け取り、値を引数に設定します:

display.max_rows

set_option()を使用して、表示するデフォルト行数を変更できます。

 import pandas as pd
 pd.set_option("display.max_rows",80)
 print(pd.get_option("display.max_rows"))

実行結果:

    80

display.max_columns

set_option()を使用して、表示するデフォルト行数を変更できます。

 import pandas as pd
 pd.set_option("display.max_columns",30)
 print(pd.get_option("display.max_columns"))

実行結果:

    30

reset_option(param)

reset_option 引数を1つ受け取り、デフォルト値に設定します。

display.max_rows

reset_option()を使用して、表示するデフォルト行数に変更を戻すことができます。

 import pandas as pd
 pd.reset_option("display.max_rows")
 print(pd.get_option("display.max_rows"))

実行結果:

    60

describe_option(param)

describe_option パラメータの説明を印刷します

display.max_rows

reset_option()を使用して、表示するデフォルト行数に変更を戻すことができます。

 import pandas as pd
 pd.describe_option("display.max_rows")

実行結果:

    display.max_rows : int
    max_rowsが超えた場合、カットオフビューに切り替えます。display.max_rowsに依存して
    'large_repr' オブジェクトは中央からカットオフされたり、印刷されます
    要約ビュー。'None'値は無制限を意味します。
    Pythonの場合/IPythonがターミナルで実行されており、`large_repr`
    equals 'truncate' これは0に設定できるため、pandasは自動で設定します-検出
    ターミナルの高さと、fitするカットオブオブジェクトを印刷します
    スクリーン高さ。IPythonノートブック、IPython qtconsole、または
    IDLEはターミナルで実行されず、したがって実行することができません
    正確自動-検出。
    [default: 60] [currently: 60]

option_context()

option_context()上下文管理器は、with文内で一時的にオプションを設定するために使用されます。withブロックから退出したとき、オプションの値が自動的に復元されます。

display.max_rows

option_context()を使用して、一時的に値を設定できます。

 import pandas as pd
 with pd.option_context("display.max_rows",10)
    print(pd.get_option("display.max_rows"))
    print(pd.get_option("display.max_rows"))

実行結果:

    10
 10

最初と第二のプリント文の間の差を確認してください。最初のプリント文はoption_context()で設定された値をプリントし、その値はwith文の内部で一時的です。with文の後、第二のプリント文は設定された値をプリントします。

よく使われるパラメータ

パラメータ説明
display.max_rows表示する最大行数
display.max_columns<表示する最大列数
display.expand_frame_reprデータフレームをページを伸ばして表示
display.max_colwidth最大列幅を表示
display.precision十進数の精度を表示