服务器之家

服务器之家 > 正文

浅谈pandas中DataFrame关于显示值省略的解决方法

时间:2021-01-29 00:08     来源/作者:晓东邪

python的pandas库是一个非常好的工具,里面的dataframe更是常用且好用,最近是越用越觉得设计的漂亮,pandas的很多细节设计的都非常好,有待使用过程中发掘。

好了,发完感慨,说一下最近dataframe遇到的一个细节:

在使用dataframe中有时候会遇到表格中的value显示不完全,像下面这样:

?
1
2
3
4
5
in
import pandas as pd
longstring = u'''真正的科学家应当是个幻想家;谁不是幻想家,谁就只能把自己称为实践家。人生的磨难是很多的,
所以我们不可对于每一件轻微的伤害都过于敏感。在生活磨难面前,精神上的坚强和无动于衷是我们抵抗罪恶和人生意外的最好武器。'''
pd.dataframe({'word':[longstring]})

输出如下:

浅谈pandas中DataFrame关于显示值省略的解决方法

可以看到,显示值长度为50个后就出现了省略了,这个因为dataframe默认的显示长度为50,不过可以改默认设置:

?
1
2
pd.set_option('max_colwidth',200)
pd.dataframe({'word':[longstring]})

浅谈pandas中DataFrame关于显示值省略的解决方法

通过设置就可以改变显示长度了。

关于set_option所有的参数介绍如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
available options:
- display.[chop_threshold, colheader_justify, column_space, date_dayfirst,
 date_yearfirst, encoding, expand_frame_repr, float_format, height, large_repr]
- display.latex.[escape, longtable, repr]
- display.[line_width, max_categories, max_columns, max_colwidth,
 max_info_columns, max_info_rows, max_rows, max_seq_items, memory_usage,
 mpl_style, multi_sparse, notebook_repr_html, pprint_nest_depth, precision,
 show_dimensions]
- display.unicode.[ambiguous_as_wide, east_asian_width]
- display.[width]
- io.excel.xls.[writer]
- io.excel.xlsm.[writer]
- io.excel.xlsx.[writer]
- io.hdf.[default_format, dropna_table]
- mode.[chained_assignment, sim_interactive, use_inf_as_null]
parameters
----------
pat : str
 regexp which should match a single option.
 note: partial matches are supported for convenience, but unless you use the
 full option name (e.g. x.y.z.option_name), your code may break in future
 versions if new options with similar names are introduced.
value :
 new value of option.
returns
-------
none
raises
------
optionerror if no such option exists
notes
-----
the available options with its descriptions:
display.chop_threshold : float or none
 if set to a float value, all float values smaller then the given threshold
 will be displayed as exactly 0 by repr and friends.
 [default: none] [currently: none]
display.colheader_justify : 'left'/'right'
 controls the justification of column headers. used by dataframeformatter.
 [default: right] [currently: right]
display.column_space no description available.
 [default: 12] [currently: 12]
display.date_dayfirst : boolean
 when true, prints and parses dates with the day first, eg 20/01/2005
 [default: false] [currently: false]
display.date_yearfirst : boolean
 when true, prints and parses dates with the year first, eg 2005/01/20
 [default: false] [currently: false]
display.encoding : str/unicode
 defaults to the detected encoding of the console.
 specifies the encoding to be used for strings returned by to_string,
 these are generally strings meant to be displayed on the console.
 [default: utf-8] [currently: utf-8]
display.expand_frame_repr : boolean
 whether to print out the full dataframe repr for wide dataframes across
 multiple lines, `max_columns` is still respected, but the output will
 wrap-around across multiple "pages" if its width exceeds `display.width`.
 [default: true] [currently: true]
display.float_format : callable
 the callable should accept a floating point number and return
 a string with the desired format of the number. this is used
 in some places like seriesformatter.
 see formats.format.engformatter for an example.
 [default: none] [currently: none]
display.height : int
 deprecated.
 [default: 60] [currently: 60]
 (deprecated, use `display.max_rows` instead.)
display.large_repr : 'truncate'/'info'
 for dataframes exceeding max_rows/max_cols, the repr (and html repr) can
 show a truncated table (the default from 0.13), or switch to the view from
 df.info() (the behaviour in earlier versions of pandas).
 [default: truncate] [currently: truncate]
display.latex.escape : bool
 this specifies if the to_latex method of a dataframe uses escapes special
 characters.
 method. valid values: false,true
 [default: true] [currently: true]
display.latex.longtable :bool
 this specifies if the to_latex method of a dataframe uses the longtable
 format.
 method. valid values: false,true
 [default: false] [currently: false]
display.latex.repr : boolean
 whether to produce a latex dataframe representation for jupyter
 environments that support it.
 (default: false)
 [default: false] [currently: false]
display.line_width : int
 deprecated.
 [default: 80] [currently: 80]
 (deprecated, use `display.width` instead.)
display.max_categories : int
 this sets the maximum number of categories pandas should output when
 printing out a `categorical` or a series of dtype "category".
 [default: 8] [currently: 8]
display.max_columns : int
 if max_cols is exceeded, switch to truncate view. depending on
 `large_repr`, objects are either centrally truncated or printed as
 a summary view. 'none' value means unlimited.
 in case python/ipython is running in a terminal and `large_repr`
 equals 'truncate' this can be set to 0 and pandas will auto-detect
 the width of the terminal and print a truncated object which fits
 the screen width. the ipython notebook, ipython qtconsole, or idle
 do not run in a terminal and hence it is not possible to do
 correct auto-detection.
 [default: 20] [currently: 20]
display.max_colwidth : int
 the maximum width in characters of a column in the repr of
 a pandas data structure. when the column overflows, a "..."
 placeholder is embedded in the output.
 [default: 50] [currently: 200]
display.max_info_columns : int
 max_info_columns is used in dataframe.info method to decide if
 per column information will be printed.
 [default: 100] [currently: 100]
display.max_info_rows : int or none
 df.info() will usually show null-counts for each column.
 for large frames this can be quite slow. max_info_rows and max_info_cols
 limit this null check only to frames with smaller dimensions than
 specified.
 [default: 1690785] [currently: 1690785]
display.max_rows : int
 if max_rows is exceeded, switch to truncate view. depending on
 `large_repr`, objects are either centrally truncated or printed as
 a summary view. 'none' value means unlimited.
 in case python/ipython is running in a terminal and `large_repr`
 equals 'truncate' this can be set to 0 and pandas will auto-detect
 the height of the terminal and print a truncated object which fits
 the screen height. the ipython notebook, ipython qtconsole, or
 idle do not run in a terminal and hence it is not possible to do
 correct auto-detection.
 [default: 60] [currently: 60]
display.max_seq_items : int or none
 when pretty-printing a long sequence, no more then `max_seq_items`
 will be printed. if items are omitted, they will be denoted by the
 addition of "..." to the resulting string.
 if set to none, the number of items to be printed is unlimited.
 [default: 100] [currently: 100]
display.memory_usage : bool, string or none
 this specifies if the memory usage of a dataframe should be displayed when
 df.info() is called. valid values true,false,'deep'
 [default: true] [currently: true]
display.mpl_style : bool
 setting this to 'default' will modify the rcparams used by matplotlib
 to give plots a more pleasing visual style by default.
 setting this to none/false restores the values to their initial value.
 [default: none] [currently: none]
display.multi_sparse : boolean
 "sparsify" multiindex display (don't display repeated
 elements in outer levels within groups)
 [default: true] [currently: true]
display.notebook_repr_html : boolean
 when true, ipython notebook will use html representation for
 pandas objects (if it is available).
 [default: true] [currently: true]
display.pprint_nest_depth : int
 controls the number of nested levels to process when pretty-printing
 [default: 3] [currently: 3]
display.precision : int
 floating point output precision (number of significant digits). this is
 only a suggestion
 [default: 6] [currently: 6]
display.show_dimensions : boolean or 'truncate'
 whether to print out dimensions at the end of dataframe repr.
 if 'truncate' is specified, only print out the dimensions if the
 frame is truncated (e.g. not display all rows and/or columns)
 [default: truncate] [currently: truncate]
display.unicode.ambiguous_as_wide : boolean
 whether to use the unicode east asian width to calculate the display text
 width.
 enabling this may affect to the performance (default: false)
 [default: false] [currently: false]
display.unicode.east_asian_width : boolean
 whether to use the unicode east asian width to calculate the display text
 width.
 enabling this may affect to the performance (default: false)
 [default: false] [currently: false]
display.width : int
 width of the display in characters. in case python/ipython is running in
 a terminal this can be set to none and pandas will correctly auto-detect
 the width.
 note that the ipython notebook, ipython qtconsole, or idle do not run in a
 terminal and hence it is not possible to correctly detect the width.
 [default: 80] [currently: 80]
io.excel.xls.writer : string
 the default excel writer engine for 'xls' files. available options:
 'xlwt' (the default).
 [default: xlwt] [currently: xlwt]
io.excel.xlsm.writer : string
 the default excel writer engine for 'xlsm' files. available options:
 'openpyxl' (the default).
 [default: openpyxl] [currently: openpyxl]
io.excel.xlsx.writer : string
 the default excel writer engine for 'xlsx' files. available options:
 'xlsxwriter' (the default), 'openpyxl'.
 [default: xlsxwriter] [currently: xlsxwriter]
io.hdf.default_format : format
 default format writing format, if none, then
 put will default to 'fixed' and append will default to 'table'
 [default: none] [currently: none]
io.hdf.dropna_table : boolean
 drop all nan rows when appending to a table
 [default: false] [currently: false]
mode.chained_assignment : string
 raise an exception, warn, or no action if trying to use chained assignment,
 the default is warn
 [default: warn] [currently: warn]
mode.sim_interactive : boolean
 whether to simulate interactive mode for purposes of testing
 [default: false] [currently: false]
mode.use_inf_as_null : boolean
 true means treat none, nan, inf, -inf as null (old way),
 false means none and nan are null, but inf, -inf are not null
 (new way).
 [default: false] [currently: false]

以上这篇浅谈pandas中dataframe关于显示值省略的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xiaodongxiexie/article/details/70147683

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部