当前位置: 代码网 > it编程>编程语言>其他编程 > Pandas设置数据显示格式实现方式

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

2025年10月23日 其他编程 我要评论
pandas设置数据显示格式在用 pandas 做数据分析的过程中,总需要打印数据分析的结果,如果数据体量较大就会存在输出内容不全(部分内容省略)或者换行错误等问题。pandas 为了解决上述问题,允

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_option("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_option("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_rows"))

输出结果:

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]

option_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_frame_repr输出数据宽度超过设置宽度时,表示是否对其要折叠,false不折叠,true要折叠。
display.max_colwidth单列数据宽度,以字符个数计算,超过时用省略号表示。
display.precision设置输出数据的小数点位数。
display.width数据显示区域的宽度,以总字符数计算。
display.show_dimensions当数据量大需要以truncate(带引号的省略方式)显示时,该参数表示是否在最后显示数据的维数,默认 true 显示,false 不显示。

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com