pandas-显示设置

发布时间 2023-12-10 01:22:03作者: 贝壳里的星海

pandas-显示设置

参数名 作用
describe_option() 查看设置的参数列表
pd.pandas.get_option('参数名', 参数值) 获得设置参数
pd.pandas.set_option('参数名', 参数值) 设置相关显示选项
pd.pandas.reset_option('参数名', 参数值) 恢复默认相关选项
max_rows 最大显示行数
max_columns 最大显示列数
max_colwidth 单列数据宽度
precision 设置输出数据小数点的位数
float_format 数字格式化

describe_option()

pandas.describe_option(pat=None)
# 显示所有选项的描述信息
# 详细参考:
# https://pandas.pydata.org/docs/reference/api/pandas.describe_option.html#pandas-describe-option

print(pd.describe_option())

常用设置举例


display.width:		# 数据显示区域的总宽度,以总字符数计算。
display.max_rows:	# 最大显示行数,超过该值用省略号代替,为None时显示所有行。
display.max_columns:# 最大显示列数,超过该值用省略号代替,为None时显示所有列。
display.max_colwidth:# 单列数据宽度,以字符个数计算,超过时用省略号表示。
display.precision:	# 设置输出数据小数点的位数。
display.expand_frame_repr:# 输出数据宽度超过设置宽度时,是否要折叠,False不折叠(通常选这个),True要折叠。

display.large_repr:	# 当数据维度超过max_rows和max_columns时,设置数据的显示方式,参数值truncate显示带省略号的数据(默认方式);
					参数值info显示数据的统计信息,而不直接显示数据(info实际就是df.info()函数)。
display.max_info_columns:df.info()# 函数按列统计每列的非空数据个数,当数据很大时,计算过程非常慢,该参数设置最大列数,当数据表的列数小于该值时,才计算每列的非空值并输出,当超过该值时不进行计算。
display.show_dimensions:# 当大的数据以truncate(带引号的省略方式)显示时,是否在最后显示数据的维数,True是显示(默认),False是不显示。

get_option()

获取具体设置的结果

# importing the module
import pandas as pd
 
# fetching maximum number of
# columns that can be displayed
print("max_columns : " +str(pd.get_option("display.max_columns")))
 
# fetching maximum number of
# rows that can be displayed
print("max_rows : " +str(pd.get_option("display.max_rows")))
 
# fetching minimum number of
# rows that can be displayed
print("min_rows : " +str(pd.get_option("display.min_rows")))


max_columns : 0
max_rows : 60
min_rows : 10
# 使用方法
import pandas as pd
pd.set_option()
pd.get_option()

# 使用属性,例如展示的最大行数
pd.option.display.max_rows

定义显示行数和列数

pd.set_option('display.max_rows', None)   	 # 显示所有行
pd.set_option('display.max_columns', None)   # 显示所有列

pd.set_option('display.max_rows',5)      # 设置最大行数
pd.set_option('display.max_columns',5)   # 设置最大列数

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3))

pd.set_option("display.max_rows", 4)
print(df)

pd.set_option("display.max_rows", 3)
print(df)

pd.reset_option("display.max_rows")
print(df)

#            0         1         2
# 0   0.598994  1.370993 -1.197167
# 1  -0.815619 -0.859034 -0.670482
# ..       ...       ...       ...
# 3  -0.451900 -0.773530  0.435744
# 4   0.769483  0.203758 -0.656397

# [5 rows x 3 columns]
#            0         1         2
# 0   0.598994  1.370993 -1.197167
# ..       ...       ...       ...
# 4   0.769483  0.203758 -0.656397

# [5 rows x 3 columns]
#           0         1         2
# 0  0.598994  1.370993 -1.197167
# 1 -0.815619 -0.859034 -0.670482
# 2  0.662157  0.506859 -0.002152
# 3 -0.451900 -0.773530  0.435744
# 4  0.769483  0.203758 -0.656397

定义列宽

pd.set_option('max_colwidth', None)   		# 自定义列宽
pd.set_option('display.width', 80)   		# 横向最多显示多少个字符;	
pd.set_option('expand_frame_repr', False)   # 禁止换行

display.max_columns
display.max_colwidth
display.expand_frame_repr

字符对齐

# 字符对齐
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

# 获取输出数据宽度超过设置宽度时,表示是否对其要折叠,False不折叠,True要折叠。
pd.get_option('display.expand_frame_repr')	

定义显示精度

pd.set_option( 'display.precision',2)
# pd.options.display.precision = 2

定义数字格式化

pd.set_option('display.float_format','{:,}'.format)

# 设置数字精度
pd.set_option('display.float_format',  '{:,.2f}'.format)

# 百分号格式化
pd.set_option('display.float_format', '{:.2f}%'.format)
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3))
print(df)
pd.set_option( 'display.precision',2)
print(df)

pd.set_option('display.float_format', '{:.2f}%'.format)

print(df)

#           0         1         2
# 0  0.631399 -0.881408  1.719873
# 1 -0.609200  1.178595 -0.779754
# 2  0.810761  1.694978 -0.279431
# 3  0.632925 -0.753222  0.397152
# 4 -0.451243  0.431111  0.687484
#       0     1     2
# 0  0.63 -0.88  1.72
# 1 -0.61  1.18 -0.78
# 2  0.81  1.69 -0.28
# 3  0.63 -0.75  0.40
# 4 -0.45  0.43  0.69
#        0      1      2
# 0  0.63% -0.88%  1.72%
# 1 -0.61%  1.18% -0.78%
# 2  0.81%  1.69% -0.28%
# 3  0.63% -0.75%  0.40%
# 4 -0.45%  0.43%  0.69%

更改pandas默认绘图库

# 使用hvplot后端绘图(不使用matplotlib)
pd.options.plotting.backend = "hvplot"

参考资料

https://pandas.pydata.org/docs/user_guide/options.html 官网

https://www.jianshu.com/p/473ddefb6caf

https://www.cnblogs.com/wang_yb/p/17656039.html

https://pypandas.cn/docs/user_guide/options.html#overview