Pandas教程

Pandas 自定义选项

Pandas 自定义选项操作实例
Pandas因为提供了API来自定义行为,所以被广泛使用。
自定义API中有五个相关功如下:
    get_option() set_option() reset_option() describe_option() option_context()
    下面我们一起了解下这些方法。

    get_option(param)

    get_option接受一个参数并输出以下值:

    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接受两个参数并将值设置为参数,如下所示:

    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 接受一个参数并将其设置回默认值。

    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
       if max_rows 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 height of the terminal and print(a truncated object which fits
       the screen height. 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: 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上下文之后,第二个print语句打印配置的值。

    Frequently used Parameters

    参数 说明
    display.max_rows 显示要显示的最大行数
    display.max_columns< 显示要显示的最大列数
    display.expand_frame_repr 显示数据框以拉伸页面
    display.max_colwidth 显示最大列宽
    display.precision 显示十进制数字的精度
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4