商务网站开发的基本流程,html5的网站有哪些,网站建站免费空间,河南智能网站建设平台65_Pandas显示设置#xff08;小数位数、有效数字、最大行/列数等#xff09;
本文介绍了使用 print() 函数显示 pandas.DataFrame、pandas.Series 等时如何更改设置#xff08;小数点后位数、有效数字、最大行/列数等#xff09;。
有关如何检查、更改和重置设置值的详细…65_Pandas显示设置小数位数、有效数字、最大行/列数等
本文介绍了使用 print() 函数显示 pandas.DataFrame、pandas.Series 等时如何更改设置小数点后位数、有效数字、最大行/列数等。
有关如何检查、更改和重置设置值的详细信息请参阅下面的文章。设置更改仅在同一代码脚本内有效。它不会被永久重写并在其他代码中再次成为默认设置。即使在同一代码中您也可以临时更改 with 块中的设置。
这里说明的只是显示时的设置原始数据值本身不会改变。如果您想对数字进行四舍五入或将其转换为指定格式的字符串请参阅下面的文章。
63_Pandas中数字的四舍五入
导入以下库。 NumPy 用于生成 pandas.DataFrame。请注意根据 pandas 的版本设置的默认值可能会有所不同。
import pandas as pd
import numpy as npprint(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)
# 6s_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: float64print(s_decimal[5])
# 0.00123456根据display. precision的设置值格式显示格式发生变化并变为指数表示法。
pd.options.display.precision 4print(s_decimal)
# 0 123.4560
# 1 12.3456
# 2 1.2346
# 3 0.1235
# 4 0.0123
# 5 0.0012
# dtype: float64pd.options.display.precision 2print(s_decimal)
# 0 1.23e02
# 1 1.23e01
# 2 1.23e00
# 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)
# Nonedisplay.float_format 指定一个可调用对象函数、方法等该对象将浮点float类型转换为任何格式的字符串。基本上您可以考虑指定字符串方法format()。
格式规范字符串’.[位数]f’可用于指定小数点后的位数.[位数]g’可用于指定总位数有效数字 。
pd.options.display.float_format {:.2f}.formatprint(s_decimal)
# 0 123.46
# 1 12.35
# 2 1.23
# 3 0.12
# 4 0.01
# 5 0.00
# dtype: float64pd.options.display.float_format {:.4g}.formatprint(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}.formatprint(s_decimal)
# 0 1.2346e02
# 1 1.2346e01
# 2 1.2346e00
# 3 1.2346e-01
# 4 1.2346e-02
# 5 1.2346e-03
# dtype: float64由于可以使用任何格式规范字符串因此也可以进行左对齐和百分比显示等对齐方式如下所示。关于如何指定格式等详细信息请参见上面format()的相关文章。
pd.options.display.float_format {: 10.2%}.formatprint(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}.formatprint(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}.formatprint(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)
# 60df_tall pd.DataFrame(np.arange(300).reshape((100, 3)))pd.options.display.max_rows 10print(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的值则中间部分被省略。
18_Pandas.DataFrame取得Series的头和尾head和tail
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 29print(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)
# 20df_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 10print(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 Noneprint(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)
# truncatepd.options.display.max_columns 10print(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 Trueprint(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 Falseprint(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 89print(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)
# 80pd.options.display.max_columns Noneprint(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 60print(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 Noneprint(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)
# 50df_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 80print(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 20print(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 40print(df_long_col_header)
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 0 aaaaaaaaaa
# 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... 列名显示的右对齐/左对齐display.colheader_justify
列名显示的右对齐或左对齐通过display.colheader_justify 设置。 默认为“right”。如果要将其左对齐请使用“left”。
print(pd.options.display.colheader_justify)
# rightprint(df_long_col)
# col
# 0 aaaaaaaaaa
# 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...pd.options.display.colheader_justify leftprint(df_long_col)
# col
# 0 aaaaaaaaaa
# 1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# 2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...