讲完怎么构造基本数据结构,不忙讲复杂函数和操作,先讲讲怎么让数据呈现的更符合心意更重要。
!cd
import numpy as np
import pandas as pd
一般来说,对我们有用的get_option和set_option操作不多,主要有以下三种关键字:
'max_rows'
¶控制可以显示的最大行数。
pd.get_option('display.max_rows')# 60的意思就是最多显示60行,如果行数超过60,那么将省略显示一部分。
60
# pd.set_option('display.max_rows', 61)
pd.Series( index = range(0,60) ) # 把60改成61试试,然后将上面注释取消,再试试。
0 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 NaN 6 NaN 7 NaN 8 NaN 9 NaN 10 NaN 11 NaN 12 NaN 13 NaN 14 NaN 15 NaN 16 NaN 17 NaN 18 NaN 19 NaN 20 NaN 21 NaN 22 NaN 23 NaN 24 NaN 25 NaN 26 NaN 27 NaN 28 NaN 29 NaN .. 31 NaN 32 NaN 33 NaN 34 NaN 35 NaN 36 NaN 37 NaN 38 NaN 39 NaN 40 NaN 41 NaN 42 NaN 43 NaN 44 NaN 45 NaN 46 NaN 47 NaN 48 NaN 49 NaN 50 NaN 51 NaN 52 NaN 53 NaN 54 NaN 55 NaN 56 NaN 57 NaN 58 NaN 59 NaN 60 NaN Length: 61, dtype: float64
'max_columns'
¶控制可以显示的最大列数,这个参数对DataFrame更有价值。
pd.get_option('display.max_columns')
20
# pd.set_option('display.max_columns', 21)
pd.DataFrame( columns = range(0,20)) # 分别设置20和21试试,然后将上面注释取消,再试试。
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
---|
'max_colwidth'
¶控制每个网格点能够显示的最大字符数
pd.get_option('display.max_colwidth')
50
# pd.set_option('display.max_colwidth', 51)
s = '1'*50 # 把50改成49试试,把上面的注释取消,再试试
pd.Series( data=[s], index = ['1']) #实际应用中把max_colwidth大体设得大一点就可以
1 11111111111111111111111111111111111111111111111111 dtype: object
'precision'
¶控制浮点类型显示的小数位数,不影响实际精度。
pd.get_option('display.precision')
6
# pd.set_option('display.precision', 10)
a = 1.01010101
s = pd.Series( data= [a]) #
s
0 1.01010101 dtype: float64
s[0] # 在Series数据被截断显示,但实际上精度并没变, 把上面的注释取消,再试试
1.0101010100000001
'colheader_justify'
¶控制DataFrame的列名对齐位置,靠左或者靠右
pd.get_option('display.colheader_justify')
'right'
# pd.set_option('display.colheader_justify','left') # 我的这个设置好像无效,不知道咋回事
pd.DataFrame( data = ['000000000000000'], columns = ['a'])
a | |
---|---|
0 | 000000000000000 |