开发者

Pandas设置数据显示格式实现方式

目录
  • Pandas设置数据显示格式
    • get_option()
    • set_option()
    • reset_option()
    • describe_option()
    • option_context()
    • 常用参数项
  • 总结

    Pandas设置数据显示格式

    在用 Pandas 做数据分析的过程中,总需要打印数据分析的结果,如果数据体量较大就会存在输出内容不全(部分内容省略)或者换行错误等问题。

    Pandas 为了解决上述问题,允许你对数据显示格式进行设置。

    下面列出了五个用来设置显示格式的函数,分别是:

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

    它们的功能介绍如下:

    函数名称说明
    get_option获取解释器的默认参数值。
    set_option更改解释器的默认参数值。
    reset_option解释器的参数重置为默认值。
    describe_option输出参数的描述信息。
    option_context临时设置解释器参数,当退出使用的语句块时,恢复为默认值。

    下面对上述函数分别进行介绍。

    get_option()

    该函数接受单一参数,用来获取显示上限的行数或者列数,示例如下:

    1) display.max_rows

    获取显示上限的行数,示例如下:

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

    输出结果:

    60
    

    2) display.max_columns

    获取显示上限的列数,示例如下:

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

    输出结果:

    20
    

    由此可知,默认值显示上限是(60,20)。

    set_option()

    该函数用来更改要默认显示的行数和列数,示例如下:

    1) 修改默认行数

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

    输出结果:

    70
    

    2) 修改默认列数

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

    输出结果:

    40
    

    reset_option()

    该方法接受一个参数,并将修改后的值设置回默认值。示例如下:

    import pandas as pd
    pd.reset_option("display.max_rows")
    #恢复为默认值
    print(pd.get_option("display.max_rowhttp://www.devze.coms"))
    

    输出结果:

    60
    

    describe_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]
    

    opti编程on_context()

    option_context() 上下文管理器,用于临时设置 with 语句块中的默认显示参数。当您退出 with 语句块时,参数值会自动恢复。示例如下:

    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
    60
    

    注意:第一个 Print 语句打印 option_context() 设置的临时值。当退出 with 语句块时,第二个 Print语句打印解释器默认值。

    常用参数项

    最后,对上述函数常用的参数项做以下总结:

    参数说明
    display.max_rows最大显示行数,超过该值用省略号代替,为None时显示所有行。
    display.max_columns最大显示列数,超过该值用省略号代替,为None时显示所有列。
    display.expand_f编程rame_repr输出数据宽度超过设置宽度时,表示是否对其要折叠,False不折叠,True要折叠。
    display.max_colwidth单列数据宽度,以字符个数计算,超过时用省略号表示。
    display.precision设置输出数据的小数点位数。
    display.width数据显示区域的宽度,以总字符数计算。
    display.show_dimensions当数据量大需要以truncate(带引号的省略方式)显示时,该参数表示是否在最后显示数据的维数,默认 True 显示,False 不显示。

    上述参数项,基本上可以满足我们的日常需求。

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜