Skip to content

Pandas: Abrufen und Setzen von Optionen für die Anzeige, das Datenverhalten usw.

Python

Bei Pandas können Sie das globale Verhalten, z. B. das Anzeigeformat, anpassen, frei von Optionen festlegen.

Dieser Artikel hat folgenden Inhalt.

  • Abrufen und Festlegen von Optionswerten mit Attribut:options
  • Drucken Sie die Beschreibung für Optionen:describe_option()
  • Abrufen und Festlegen von Optionswerten mit Funktionen:get_option(), set_option()
  • Abrufen und Festlegen mehrerer Optionswerte
  • Optionen auf Standardwerte zurücksetzen:reset_option()
  • Einstellungen vorübergehend ändern mit:option_context()

Beachten Sie, dass das Ändern von Optionen this nicht permanent neu schreibt; Ein anderer Code verwendet wieder die Standardeinstellungen.

Die Pandas-Version in diesem Beispielcode lautet wie folgt. Beachten Sie, dass pprint used WIRD, um die Anzeige leichter lesbar zu machen.

import pandas as pd
import pprint

print(pd.__version__)
# 0.23.0

Abrufen und Festlegen von Optionswerten mit Attribut:options

Sie können die Optionswerte über die Attribute unter pd.options abrufen und festlegen.

print(pd.options.display.max_rows)
# 60

pd.options.display.max_rows = 100

print(pd.options.display.max_rows)
# 100

Sie können mit der eingebauten Funktion dir() prüfen, welche Elemente verfügbar sind.

print(dir(pd.options))
# ['compute', 'display', 'html', 'io', 'mode', 'plotting']

pprint.pprint(dir(pd.options.display))
# ['chop_threshold',
#  'colheader_justify',
#  'column_space',
#  'date_dayfirst',
#  'date_yearfirst',
#  'encoding',
#  'expand_frame_repr',
#  'float_format',
#  'html',
#  'large_repr',
#  'latex',
#  'max_categories',
#  'max_columns',
#  'max_colwidth',
#  'max_info_columns',
#  'max_info_rows',
#  'max_rows',
#  'max_seq_items',
#  'memory_usage',
#  'multi_sparse',
#  'notebook_repr_html',
#  'pprint_nest_depth',
#  'precision',
#  'show_dimensions',
#  'unicode',
#  'width']

Drucken Sie die Beschreibung für Optionen:describe_option()

Sie können die Beschreibung, den Standardwert und den aktuellen Wert jeder Option mit der Funktion pd.describe_option() drucken.

Wenn das Argument weggelassen WIRD, Werden Informationen zu allen Optionen angezeigt. Die Ausgabe entfällt hier.

Sie können für das erste Argument eine Musterzeichenfolge für einen regulären Ausdruck angeben. Dem Muster entsprechende Optionen werden angezeigt. [Standard: xxx] [derzeit: xxx] repräsentiert die Standard- und aktuellen Werte.

pd.describe_option('max.*col')
# display.max_columns : int
#     If max_cols is exceeded, switch to truncate view. Depending on
#     `large_repr`, objects are either centrally truncated or printed as
#     a summary view. 'None' value means unlimited.
#     In case python/IPython is running in a terminal and `large_repr`
#     equals 'truncate' this can be set to 0 and pandas will auto-detect
#     the width of the terminal and print a truncated object which fits
#     the screen width. The IPython notebook, IPython qtconsole, or IDLE
#     do not run in a terminal and hence it is not possible to do
#     correct auto-detection.
#     [default: 20] [currently: 20]
# display.max_colwidth : int
#     The maximum width in characters of a column in the repr of
#     a pandas data structure. When the column overflows, a "..."
#     placeholder is embedded in the output.
#     [default: 50] [currently: 50]
# display.max_info_columns : int
#     max_info_columns is used in DataFrame.info method to decide if
#     per column information will be printed.
#     [default: 100] [currently: 100]

Wenn Sie nur eine Zeichenfolge ohne Sonderzeichen des regulären Ausdrucks angeben, werden sterben Optionen angezeigt, sterben sterben Zeichenfolge enthalten.

pd.describe_option('compute')
# compute.use_bottleneck : bool
#     Use the bottleneck library to accelerate if it is installed,
#     the default is True
#     Valid values: False,True
#     [default: True] [currently: True]
# compute.use_numexpr : bool
#     Use the numexpr library to accelerate computation if it is installed,
#     the default is True
#     Valid values: False,True
#     [default: True] [currently: True]

pd.describe_option('max_col')
# display.max_columns : int
#     If max_cols is exceeded, switch to truncate view. Depending on
#     `large_repr`, objects are either centrally truncated or printed as
#     a summary view. 'None' value means unlimited.
#     In case python/IPython is running in a terminal and `large_repr`
#     equals 'truncate' this can be set to 0 and pandas will auto-detect
#     the width of the terminal and print a truncated object which fits
#     the screen width. The IPython notebook, IPython qtconsole, or IDLE
#     do not run in a terminal and hence it is not possible to do
#     correct auto-detection.
#     [default: 20] [currently: 20]
# display.max_colwidth : int
#     The maximum width in characters of a column in the repr of
#     a pandas data structure. When the column overflows, a "..."
#     placeholder is embedded in the output.
#     [default: 50] [currently: 50]

Abrufen und Festlegen von Optionswerten mit Funktionen:get_option(), set_option()

Sie können Optionswerte auch mit Funktionen abrufen und festlegen.

pd.get_option() gibt den aktuellen Optionswert zurück.

print(pd.get_option('display.max_rows'))
# 100

Sie können den Optionswert mit pd.set_option() setzen.

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

Sie können ein reguläres Ausdrucksmuster als Argument angeben. Sie müssen nicht den vollständigen Elementnamen angeben, aber der Abgleich mehrerer Elemente löst OptionError aus.

print(pd.get_option('max_r'))
# 60

pd.set_option('max_r', 100)

# pd.get_option('max')
# OptionError: 'Pattern matched multiple keys'

# pd.set_option('max', 60)
# OptionError: 'Pattern matched multiple keys'

Abrufen und Festlegen mehrerer Optionswerte

Funktionen zum Abrufen und Festlegen mehrerer Optionswerte werden nicht bereitgestellt.

Dieser Abschnitt zeigt, wie Sie mehrere Optionswerte mit Listenverständnis usw. abrufen und festlegen.

Erhalten Sie Optionswerte der aufgelisteten Elementnamen mit Listen- und Diktatverständnis.

l = ['display.max_rows', 'display.max_columns', 'display.max_colwidth']

print([pd.get_option(i) for i in l])
# [100, 20, 50]

print({i: pd.get_option(i) for i in l})
# {'display.max_rows': 100, 'display.max_columns': 20, 'display.max_colwidth': 50}

Legen Sie Optionswerte basierend auf dem Wörterbuch der Elementnamen und dem Wert mit den Wörterbuchmethoden items() und keys() fest.

d = {'display.max_rows': 80,
     'display.max_columns': 80,
     'display.max_colwidth': 80}

[pd.set_option(k, v) for k, v in d.items()]

print({i: pd.get_option(i) for i in d.keys()})
# {'display.max_rows': 80, 'display.max_columns': 80, 'display.max_colwidth': 80}

Optionen auf Standardwerte zurücksetzen:reset_option()

reset_option() setzt Optionen auf Standardwerte zurück.

Sie können eine Musterzeichenfolge für reguläre Ausdrücke angeben.

print(pd.options.display.max_rows)
# 80

pd.reset_option('display.max_rows')

print(pd.options.display.max_rows)
# 60

Wenn mehrere Artikel übereinstimmen, werden alle Artikel zurückgesetzt.

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 80
# 80

pd.reset_option('max_col')

print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 20
# 50

Indem can SIE alle angezeigten Optionen mit dem regulären Ausdrucks-Sonderzeichen ^ zurücksetzen, das zum Anfang passt.

pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
pd.options.display.max_colwidth = 100

pd.reset_option('^display', silent=True)

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
print(pd.options.display.max_colwidth)
# 60
# 20
# 50

‚all‘ setzt alle Elemente zurück.

pd.reset_option('all')
# html.border has been deprecated, use display.html.border instead
# (currently both are identical)
# : boolean
#     use_inf_as_null had been deprecated and will be removed in a future
#     version. Use `use_inf_as_na` instead.
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning: html.border has been deprecated, use display.html.border instead
# (currently both are identical)
#   warnings.warn(d.msg, FutureWarning)
# /usr/local/lib/python3.6/site-packages/pandas/core/config.py:619: FutureWarning: 
# : boolean
#     use_inf_as_null had been deprecated and will be removed in a future
#     version. Use `use_inf_as_na` instead.
#   warnings.warn(d.msg, FutureWarning)

Wenn FutureWarning nicht ausgegeben werden soll, geben Sie silent=True an.

pd.reset_option('all', silent=True)

Einstellungen vorübergehend ändern mit:option_context()

Mit pd.option_context() werden Einstellungen nur im with-Block geändert.

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
# 100

print(pd.options.display.max_rows)
# 60

Es wird nicht auf den Standardwert zurückgesetzt, sondern auf den Wert vor dem with-Block zurückgesetzt.

pd.options.display.max_rows = 80

with pd.option_context('display.max_rows', 100):
    print(pd.options.display.max_rows)
# 100

print(pd.options.display.max_rows)
# 80

Mehrere Elemente können geändert werden, wodurch wiederholt eine Musterzeichenfolge für einen regulären Ausdruck und ein neuer Wert wie (pat, val, val, …) angegeben werden.

with pd.option_context('display.max_rows', 100, 'display.max_columns', 100):
    print(pd.options.display.max_rows)
    print(pd.options.display.max_columns)
# 100
# 100

print(pd.options.display.max_rows)
print(pd.options.display.max_columns)
# 80
# 20