網(wǎng)站服務(wù)合同用交印花稅嗎點(diǎn)擊器 百度網(wǎng)盤
65_Pandas顯示設(shè)置(小數(shù)位數(shù)、有效數(shù)字、最大行/列數(shù)等)
本文介紹了使用 print() 函數(shù)顯示 pandas.DataFrame、pandas.Series 等時(shí)如何更改設(shè)置(小數(shù)點(diǎn)后位數(shù)、有效數(shù)字、最大行/列數(shù)等)。
有關(guān)如何檢查、更改和重置設(shè)置值的詳細(xì)信息,請(qǐng)參閱下面的文章。設(shè)置更改僅在同一代碼(腳本)內(nèi)有效。它不會(huì)被永久重寫,并在其他代碼中再次成為默認(rèn)設(shè)置。即使在同一代碼中,您也可以臨時(shí)更改 with 塊中的設(shè)置。
這里說明的只是顯示時(shí)的設(shè)置,原始數(shù)據(jù)值本身不會(huì)改變。如果您想對(duì)數(shù)字進(jìn)行四舍五入或?qū)⑵滢D(zhuǎn)換為指定格式的字符串,請(qǐng)參閱下面的文章。
- 63_Pandas中數(shù)字的四舍五入
導(dǎo)入以下庫。 NumPy 用于生成 pandas.DataFrame。請(qǐng)注意,根據(jù) pandas 的版本,設(shè)置的默認(rèn)值可能會(huì)有所不同。
import pandas as pd
import numpy as npprint(pd.__version__)
# 0.23.0
這里我們將解釋與顯示相關(guān)的主要項(xiàng)目。
- 小數(shù)點(diǎn)右邊的位數(shù):display. precision
- 有效數(shù)字:display.float_format
- 關(guān)于四舍五入的注意事項(xiàng)
- 最大顯示行數(shù):display.max_rows
- 最大顯示列數(shù):display.max_columns
- 默認(rèn)顯示的行數(shù)和列數(shù):display.show_dimensions
- 總體最大顯示寬度:display.width
- 每列最大顯示寬度:display.max_colwidth
- 列名顯示的右對(duì)齊/左對(duì)齊:display.colheader_justify
小數(shù)點(diǎn)右邊的位數(shù):display. precision
小數(shù)點(diǎn)后的位數(shù)用display. precision設(shè)置。 默認(rèn)為 6,無論整數(shù)部分有多少位,小數(shù)點(diǎn)以下的位數(shù)都將是指定的數(shù)字。盡管被省略并顯示,原始數(shù)據(jù)值也保留了后續(xù)數(shù)字的信息。
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
根據(jù)display. precision的設(shè)置值,格式(顯示格式)發(fā)生變化并變?yōu)橹笖?shù)表示法。
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.23e+02
# 1 1.23e+01
# 2 1.23e+00
# 3 1.23e-01
# 4 1.23e-02
# 5 1.23e-03
# dtype: float64
如果要控制格式,請(qǐng)使用 display.float_format,如下所述。
有效數(shù)字:display.float_format
用display. precision可以設(shè)置的是小數(shù)點(diǎn)后的位數(shù),如果想指定包括整數(shù)部分在內(nèi)的有效數(shù)字(significantdigits)的個(gè)數(shù),則使用display.float_format。默認(rèn)為“None”。
print(pd.options.display.float_format)
# None
display.float_format 指定一個(gè)可調(diào)用對(duì)象(函數(shù)、方法等),該對(duì)象將浮點(diǎn)float類型轉(zhuǎn)換為任何格式的字符串?;旧?#xff0c;您可以考慮指定字符串方法format()。
格式規(guī)范字符串’.[位數(shù)]f’可用于指定小數(shù)點(diǎn)后的位數(shù),'.[位數(shù)]g’可用于指定總位數(shù)(有效數(shù)字) )。
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
如果要顯示相同的位數(shù),請(qǐng)使用“.[位數(shù)]e”來使用指數(shù)表示法。由于整數(shù)部分始終為 1 位,因此有效數(shù)字為設(shè)定的位數(shù) + 1。
pd.options.display.float_format = '{:.4e}'.formatprint(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
由于可以使用任何格式規(guī)范字符串,因此也可以進(jìn)行左對(duì)齊和百分比顯示等對(duì)齊方式,如下所示。關(guān)于如何指定格式等詳細(xì)信息,請(qǐng)參見上面format()的相關(guān)文章。
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
關(guān)于四舍五入的注意事項(xiàng)
display. precision 和 display.float_format 對(duì)值進(jìn)行四舍五入,但不是一般四舍五入,而是四舍五入為偶數(shù);例如,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
另外,在四舍五入到小數(shù)點(diǎn)時(shí),根據(jù)該值,可以四舍五入到偶數(shù),也可以四舍五入到奇數(shù)。
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
這是由于浮點(diǎn)數(shù)的處理造成的。
最大顯示行數(shù):display.max_rows
最大顯示行數(shù)通過display.max_rows 設(shè)置。如果行數(shù)超過display.max_rows的值,則省略中間部分,顯示開頭和結(jié)尾。 默認(rèn)值為 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]
如果只想顯示開頭或結(jié)尾,請(qǐng)使用 head() 或 tail()。同樣在這種情況下,如果行數(shù)超過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]
如果將其設(shè)置為“None”,則將顯示所有行而不省略。
pd.options.display.max_rows = None
最大顯示列數(shù):display.max_columns
顯示列的最大數(shù)量通過display.max_columns 設(shè)置。如果列數(shù)超過display.max_columns的值,則省略中間部分,顯示開頭和結(jié)尾。 默認(rèn)為20,如果設(shè)置為None,則將顯示所有列,不會(huì)被省略。
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設(shè)置。見下文。 另外,在終端中運(yùn)行時(shí),display.max_columns 的默認(rèn)值為 0,并且根據(jù)終端的寬度自動(dòng)省略。
默認(rèn)顯示的行數(shù)和列數(shù):display.show_dimensions
與前面的示例一樣,如果省略了行和列,則行數(shù)和列數(shù)將顯示在末尾,例如[3 行 x 30 列]。 可以使用 display.show_dimensions 配置此行為。默認(rèn)為“truncate”,只有省略時(shí)才會(huì)顯示行數(shù)和列數(shù)。
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
如果設(shè)置為True,則無論是否省略都會(huì)始終顯示,如果設(shè)置為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 設(shè)置。 默認(rèn)值為 80。如果超過該值,就會(huì)發(fā)生換行。換行符處顯示反斜杠 \,如下例所示。 即使display.width為None,也不會(huì)顯示整個(gè)圖像。
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 設(shè)置。 默認(rèn)值為 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
每一列都被省略以適應(yīng) display.max_colwidth 設(shè)置。
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...
列名顯示的右對(duì)齊/左對(duì)齊:display.colheader_justify
列名顯示的右對(duì)齊或左對(duì)齊通過d??isplay.colheader_justify 設(shè)置。 默認(rèn)為“right”。如果要將其左對(duì)齊,請(qǐng)使用“l(fā)eft”。
print(pd.options.display.colheader_justify)
# rightprint(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...