65_Pandas display settings (number of decimal places, significant digits, maximum number of rows/columns, etc.)

65_Pandas display settings (number of decimal places, significant digits, maximum number of rows/columns, etc.)

This article explains how to change settings (number of decimal places, significant digits, maximum number of rows/columns, etc.) when displaying pandas.DataFrame, pandas.Series, etc. using the print() function.

See the article below for details on how to check, change, and reset setting values. Setting changes are only valid within the same code (script). It is not permanently overridden and becomes the default again in other code. Even within the same code, you can temporarily change settings in a with block.

What is explained here is only the setting during display, the original data value itself will not change. If you want to round a number or convert it to a string in a specified format, see the article below.

Import the following libraries. NumPy is used to generate pandas.DataFrame. Note that depending on the version of pandas, the default value set may differ.

import pandas as pd
import numpy as np

print(pd.__version__)
# 0.23.0

Here we will explain the main items related to display.

  • Number of digits to the right of the decimal point: display.precision
  • Valid numbers: display.float_format
  • A note on rounding
  • Maximum number of display rows: display.max_rows
  • Maximum number of displayed columns: display.max_columns
  • The number of rows and columns displayed by default: display.show_dimensions
  • Overall maximum display width: display.width
  • Maximum display width of each column: display.max_colwidth
  • Right/left alignment of column name display: display.colheader_justify

Number of digits to the right of the decimal point: display.precision

The number of digits after the decimal point is set with display.precision. The default is 6, no matter how many digits there are in the integer part, the number of digits below the decimal point will be the specified number. Although omitted and displayed, the original data value also retains the information of subsequent numbers.

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

Depending on the setting value of display.precision, the format (display format) changes and becomes exponential notation.

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

If you want to control formatting, use display.float_format as described below.

Valid numbers: display.float_format

What you can set with display.precision is the number of digits after the decimal point. If you want to specify the number of significant digits (significantdigits) including the integer part, use display.float_format. Default is "None".

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

display.float_format specifies a callable object (function, method, etc.) that converts a floating-point float type to a string in any format. Basically, you can consider specifying the string method format().

The format specification string '.[digits]f' can be used to specify the number of digits after the decimal point, and '.[digits]g' can be used to specify the total number of digits (significant digits).

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

If you want to display the same number of digits, use ".[digits]e" to use exponential notation. Since the integer part is always 1 digit, the valid digits are the set number of digits + 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

Since any format specification string can be used, alignments such as left justification and percentage display are also possible, as shown below. For details on how to specify the format, please see the related articles on format() above.

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

A note on rounding

display.precision and display.float_format round values, but instead of rounding normally, they round to an even number; for example, 0.5 rounds to 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

Also, when rounding to a decimal point, depending on the value, it can be rounded to an even number or to an odd number.

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

This is due to the handling of floating point numbers.

Maximum number of display rows: display.max_rows

The maximum number of displayed rows is set by display.max_rows. If the number of rows exceeds the value of display.max_rows, the middle part is omitted and the beginning and end are displayed. The default value is 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]

If you want to display only the beginning or the end, use head() or tail(). Also in this case, if the number of rows exceeds the value of display.max_rows, the middle part is omitted.

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]

If set to "None" all rows will be displayed without omission.

pd.options.display.max_rows = None

Maximum number of displayed columns: display.max_columns

The maximum number of columns displayed is set by display.max_columns. If the number of columns exceeds the value of display.max_columns, the middle part is omitted and the beginning and end are displayed. Defaults to 20, if set to None all columns will be displayed and will not be omitted.

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  

The overall display width is set via display.width. see below. Additionally, when running in a terminal, the default value of display.max_columns is 0 and is automatically omitted based on the width of the terminal.

The number of rows and columns displayed by default: display.show_dimensions

As in the previous example, if rows and columns are omitted, the row and column numbers will appear at the end, for example [3 rows x 30 columns]. This behavior can be configured using display.show_dimensions. Defaults to "truncate", which displays row and column numbers only when omitted.

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

If set to True, it will always be shown whether omitted or not, if set to False, it will always be hidden.

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

Overall maximum display width: display.width

The overall maximum display width is set via display.width. The default value is 80. If this value is exceeded, a line break will occur. A backslash \ appears at the newline character, as shown in the following example. Even if display.width is None, the entire image will not be displayed.

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  

Maximum display width of each column: display.max_colwidth

The maximum display width of each column is set by display.max_colwidth. The default value is 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

Each column is omitted to accommodate the display.max_colwidth setting.

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...

Column names columns are not affected by display.max_colwidth and cannot be omitted.

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...                 

Right/left alignment of column name display: display.colheader_justify

The right or left alignment of column name display is set by display.colheader_justify. Default is "right". If you want it to be left aligned, use "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...

Guess you like

Origin blog.csdn.net/qq_18351157/article/details/133954185
Recommended