65_Pandas显示设置(小数位数、有效数字、最大行/列数等)

65_Pandas显示设置(小数位数、有效数字、最大行/列数等)

本文介绍了使用 print() 函数显示 pandas.DataFrame、pandas.Series 等时如何更改设置(小数点后位数、有效数字、最大行/列数等)。

有关如何检查、更改和重置设置值的详细信息,请参阅下面的文章。设置更改仅在同一代码(脚本)内有效。它不会被永久重写,并在其他代码中再次成为默认设置。即使在同一代码中,您也可以临时更改 with 块中的设置。

这里说明的只是显示时的设置,原始数据值本身不会改变。如果您想对数字进行四舍五入或将其转换为指定格式的字符串,请参阅下面的文章。

导入以下库。 NumPy 用于生成 pandas.DataFrame。请注意,根据 pandas 的版本,设置的默认值可能会有所不同。

import pandas as pd
import numpy as np

print(pd.__version__)
# 0.23.0

这里我们将解释与显示相关的主要项目。

  • 小数点右边的位数:display. precision
  • 有效数字:display.float_format
  • 关于四舍五入的注意事项
  • 最大显示行数:display.max_rows
  • 最大显示列数:display.max_columns
  • 默认显示的行数和列数:display.show_dimensions
  • 总体最大显示宽度:display.width
  • 每列最大显示宽度:display.max_colwidth
  • 列名显示的右对齐/左对齐:display.colheader_justify

小数点右边的位数:display. precision

小数点后的位数用display. precision设置。 默认为 6,无论整数部分有多少位,小数点以下的位数都将是指定的数字。尽管被省略并显示,原始数据值也保留了后续数字的信息。

print(pd.options.display.precision)
# 6

s_decimal = pd.Series([123.456, 12.3456, 1.23456, 0.123456, 0.0123456, 0.00123456])

print(s_decimal)
# 0    123.456000
# 1     12.345600
# 2      1.234560
# 3      0.123456
# 4      0.012346
# 5      0.001235
# dtype: float64

print(s_decimal[5])
# 0.00123456

根据display. precision的设置值,格式(显示格式)发生变化并变为指数表示法。

pd.options.display.precision = 4

print(s_decimal)
# 0    123.4560
# 1     12.3456
# 2      1.2346
# 3      0.1235
# 4      0.0123
# 5      0.0012
# dtype: float64

pd.options.display.precision = 2

print(s_decimal)
# 0    1.23e+02
# 1    1.23e+01
# 2    1.23e+00
# 3    1.23e-01
# 4    1.23e-02
# 5    1.23e-03
# dtype: float64

如果要控制格式,请使用 display.float_format,如下所述。

有效数字:display.float_format

用display. precision可以设置的是小数点后的位数,如果想指定包括整数部分在内的有效数字(significantdigits)的个数,则使用display.float_format。默认为“None”。

print(pd.options.display.float_format)
# None

display.float_format 指定一个可调用对象(函数、方法等),该对象将浮点float类型转换为任何格式的字符串。基本上,您可以考虑指定字符串方法format()。

格式规范字符串’.[位数]f’可用于指定小数点后的位数,'.[位数]g’可用于指定总位数(有效数字) )。

扫描二维码关注公众号,回复: 17214431 查看本文章
pd.options.display.float_format = '{:.2f}'.format

print(s_decimal)
# 0   123.46
# 1    12.35
# 2     1.23
# 3     0.12
# 4     0.01
# 5     0.00
# dtype: float64

pd.options.display.float_format = '{:.4g}'.format

print(s_decimal)
# 0      123.5
# 1      12.35
# 2      1.235
# 3     0.1235
# 4    0.01235
# 5   0.001235
# dtype: float64

如果要显示相同的位数,请使用“.[位数]e”来使用指数表示法。由于整数部分始终为 1 位,因此有效数字为设定的位数 + 1。

pd.options.display.float_format = '{:.4e}'.format

print(s_decimal)
# 0   1.2346e+02
# 1   1.2346e+01
# 2   1.2346e+00
# 3   1.2346e-01
# 4   1.2346e-02
# 5   1.2346e-03
# dtype: float64

由于可以使用任何格式规范字符串,因此也可以进行左对齐和百分比显示等对齐方式,如下所示。关于如何指定格式等详细信息,请参见上面format()的相关文章。

pd.options.display.float_format = '{: <10.2%}'.format

print(s_decimal)
# 0   12345.60% 
# 1   1234.56%  
# 2   123.46%   
# 3   12.35%    
# 4   1.23%     
# 5   0.12%     
# dtype: float64

关于四舍五入的注意事项

display. precision 和 display.float_format 对值进行四舍五入,但不是一般四舍五入,而是四舍五入为偶数;例如,0.5 四舍五入为 0。

df_decimal = pd.DataFrame({
    
    's': ['0.4', '0.5', '0.6', '1.4', '1.5', '1.6'],
                           'f': [0.4, 0.5, 0.6, 1.4, 1.5, 1.6]})

pd.options.display.float_format = '{:.0f}'.format

print(df_decimal)
#      s  f
# 0  0.4  0
# 1  0.5  0
# 2  0.6  1
# 3  1.4  1
# 4  1.5  2
# 5  1.6  2

另外,在四舍五入到小数点时,根据该值,可以四舍五入到偶数,也可以四舍五入到奇数。

df_decimal2 = pd.DataFrame({
    
    's': ['0.04', '0.05', '0.06', '0.14', '0.15', '0.16'],
                            'f': [0.04, 0.05, 0.06, 0.14, 0.15, 0.16]})

pd.options.display.float_format = '{:.1f}'.format

print(df_decimal2)
#       s   f
# 0  0.04 0.0
# 1  0.05 0.1
# 2  0.06 0.1
# 3  0.14 0.1
# 4  0.15 0.1
# 5  0.16 0.2

这是由于浮点数的处理造成的。

最大显示行数:display.max_rows

最大显示行数通过display.max_rows 设置。如果行数超过display.max_rows的值,则省略中间部分,显示开头和结尾。 默认值为 60。

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

df_tall = pd.DataFrame(np.arange(300).reshape((100, 3)))

pd.options.display.max_rows = 10

print(df_tall)
#       0    1    2
# 0     0    1    2
# 1     3    4    5
# 2     6    7    8
# 3     9   10   11
# 4    12   13   14
# ..  ...  ...  ...
# 95  285  286  287
# 96  288  289  290
# 97  291  292  293
# 98  294  295  296
# 99  297  298  299
# [100 rows x 3 columns]

如果只想显示开头或结尾,请使用 head() 或 tail()。同样在这种情况下,如果行数超过display.max_rows的值,则中间部分被省略。

print(df_tall.head(10))
#     0   1   2
# 0   0   1   2
# 1   3   4   5
# 2   6   7   8
# 3   9  10  11
# 4  12  13  14
# 5  15  16  17
# 6  18  19  20
# 7  21  22  23
# 8  24  25  26
# 9  27  28  29

print(df_tall.head(20))
#      0   1   2
# 0    0   1   2
# 1    3   4   5
# 2    6   7   8
# 3    9  10  11
# 4   12  13  14
# ..  ..  ..  ..
# 15  45  46  47
# 16  48  49  50
# 17  51  52  53
# 18  54  55  56
# 19  57  58  59
# [20 rows x 3 columns]

如果将其设置为“None”,则将显示所有行而不省略。

pd.options.display.max_rows = None

最大显示列数:display.max_columns

显示列的最大数量通过display.max_columns 设置。如果列数超过display.max_columns的值,则省略中间部分,显示开头和结尾。 默认为20,如果设置为None,则将显示所有列,不会被省略。

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

df_wide = pd.DataFrame(np.arange(90).reshape((3, 30)))

print(df_wide)
#    0   1   2   3   4   5   6   7   8   9  ...  20  21  22  23  24  25  26  27  \
# 0   0   1   2   3   4   5   6   7   8   9 ...  20  21  22  23  24  25  26  27   
# 1  30  31  32  33  34  35  36  37  38  39 ...  50  51  52  53  54  55  56  57   
# 2  60  61  62  63  64  65  66  67  68  69 ...  80  81  82  83  84  85  86  87   
#    28  29  
# 0  28  29  
# 1  58  59  
# 2  88  89  
# [3 rows x 30 columns]

pd.options.display.max_columns = 10

print(df_wide)
#    0   1   2   3   4  ...  25  26  27  28  29
# 0   0   1   2   3   4 ...  25  26  27  28  29
# 1  30  31  32  33  34 ...  55  56  57  58  59
# 2  60  61  62  63  64 ...  85  86  87  88  89
# [3 rows x 30 columns]

pd.options.display.max_columns = None

print(df_wide)
#    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  \
# 0   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18   
# 1  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48   
# 2  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78   
#    19  20  21  22  23  24  25  26  27  28  29  
# 0  19  20  21  22  23  24  25  26  27  28  29  
# 1  49  50  51  52  53  54  55  56  57  58  59  
# 2  79  80  81  82  83  84  85  86  87  88  89  

整体显示宽度通过display.width设置。见下文。 另外,在终端中运行时,display.max_columns 的默认值为 0,并且根据终端的宽度自动省略。

默认显示的行数和列数:display.show_dimensions

与前面的示例一样,如果省略了行和列,则行数和列数将显示在末尾,例如[3 行 x 30 列]。 可以使用 display.show_dimensions 配置此行为。默认为“truncate”,只有省略时才会显示行数和列数。

print(pd.options.display.show_dimensions)
# truncate

pd.options.display.max_columns = 10

print(df_wide)
#    0   1   2   3   4  ...  25  26  27  28  29
# 0   0   1   2   3   4 ...  25  26  27  28  29
# 1  30  31  32  33  34 ...  55  56  57  58  59
# 2  60  61  62  63  64 ...  85  86  87  88  89
# [3 rows x 30 columns]

df = pd.DataFrame(np.arange(12).reshape((3, 4)))

print(df)
#    0  1   2   3
# 0  0  1   2   3
# 1  4  5   6   7
# 2  8  9  10  11

如果设置为True,则无论是否省略都会始终显示,如果设置为False,则始终隐藏。

pd.options.display.show_dimensions = True

print(df_wide)
#    0   1   2   3   4  ...  25  26  27  28  29
# 0   0   1   2   3   4 ...  25  26  27  28  29
# 1  30  31  32  33  34 ...  55  56  57  58  59
# 2  60  61  62  63  64 ...  85  86  87  88  89
# [3 rows x 30 columns]

print(df)
#    0  1   2   3
# 0  0  1   2   3
# 1  4  5   6   7
# 2  8  9  10  11
# [3 rows x 4 columns]

pd.options.display.show_dimensions = False

print(df_wide)
#    0   1   2   3   4  ...  25  26  27  28  29
# 0   0   1   2   3   4 ...  25  26  27  28  29
# 1  30  31  32  33  34 ...  55  56  57  58  59
# 2  60  61  62  63  64 ...  85  86  87  88  89

print(df)
#    0  1   2   3
# 0  0  1   2   3
# 1  4  5   6   7
# 2  8  9  10  11

总体最大显示宽度:display.width

总体最大显示宽度通过display.width 设置。 默认值为 80。如果超过该值,就会发生换行。换行符处显示反斜杠 \,如下例所示。 即使display.width为None,也不会显示整个图像。

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

pd.options.display.max_columns = None

print(df_wide)
#    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  \
# 0   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18   
# 1  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48   
# 2  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78   
#    19  20  21  22  23  24  25  26  27  28  29  
# 0  19  20  21  22  23  24  25  26  27  28  29  
# 1  49  50  51  52  53  54  55  56  57  58  59  
# 2  79  80  81  82  83  84  85  86  87  88  89  

pd.options.display.width = 60

print(df_wide)
#    0   1   2   3   4   5   6   7   8   9   10  11  12  13  \
# 0   0   1   2   3   4   5   6   7   8   9  10  11  12  13   
# 1  30  31  32  33  34  35  36  37  38  39  40  41  42  43   
# 2  60  61  62  63  64  65  66  67  68  69  70  71  72  73   
#    14  15  16  17  18  19  20  21  22  23  24  25  26  27  \
# 0  14  15  16  17  18  19  20  21  22  23  24  25  26  27   
# 1  44  45  46  47  48  49  50  51  52  53  54  55  56  57   
# 2  74  75  76  77  78  79  80  81  82  83  84  85  86  87   
#    28  29  
# 0  28  29  
# 1  58  59  
# 2  88  89  

pd.options.display.width = None

print(df_wide)
#    0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  \
# 0   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18   
# 1  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48   
# 2  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78   
#    19  20  21  22  23  24  25  26  27  28  29  
# 0  19  20  21  22  23  24  25  26  27  28  29  
# 1  49  50  51  52  53  54  55  56  57  58  59  
# 2  79  80  81  82  83  84  85  86  87  88  89  

每列最大显示宽度:display.max_colwidth

每列的最大显示宽度通过display.max_colwidth 设置。 默认值为 50。

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

df_long_col = pd.DataFrame({
    
    'col': ['a' * 10, 'a' * 30, 'a' * 60]})

print(df_long_col)
#                                                  col
# 0                                         aaaaaaaaaa
# 1                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

pd.options.display.max_colwidth = 80

print(df_long_col)
#                                                             col
# 0                                                    aaaaaaaaaa
# 1                                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

每一列都被省略以适应 display.max_colwidth 设置。

df_long_col2 = pd.DataFrame({
    
    'col1': ['a' * 10, 'a' * 30, 'a' * 60],
                             'col2': ['a' * 10, 'a' * 30, 'a' * 60]})

pd.options.display.max_colwidth = 20

print(df_long_col2)
#                   col1                 col2
# 0           aaaaaaaaaa           aaaaaaaaaa
# 1  aaaaaaaaaaaaaaaa...  aaaaaaaaaaaaaaaa...
# 2  aaaaaaaaaaaaaaaa...  aaaaaaaaaaaaaaaa...

列名columns不受display.max_colwidth的影响,不能省略。

df_long_col_header = pd.DataFrame({
    
    'a' * 60: ['a' * 10, 'a' * 30, 'a' * 60]})

pd.options.display.max_colwidth = 40

print(df_long_col_header)
#   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 0                               aaaaaaaaaa                    
# 1           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa                    
# 2  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...                 

列名显示的右对齐/左对齐:display.colheader_justify

列名显示的右对齐或左对齐通过d​​isplay.colheader_justify 设置。 默认为“right”。如果要将其左对齐,请使用“left”。

print(pd.options.display.colheader_justify)
# right

print(df_long_col)
#                                        col
# 0                               aaaaaaaaaa
# 1           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

pd.options.display.colheader_justify = 'left'

print(df_long_col)
#   col                                     
# 0                               aaaaaaaaaa
# 1           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

猜你喜欢

转载自blog.csdn.net/qq_18351157/article/details/133954185