欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-29 07:58:42
python的pandas库是一个非常好的工具,里面的dataframe更是常用且好用,最近是越用越觉得设计的漂亮,pandas的很多细节设计的都非常好,有待使用过程中发掘...

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

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

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

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

输出如下:

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

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

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

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

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

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

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关于显示值省略的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。