pandas DataFrame.to_string() 从列中截断字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11994765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:47:48  来源:igfitidea点击:

pandas DataFrame.to_string() truncating strings from columns

pythonpandas

提问by Kunal B.

When I try to use to_string to output a column from a dataframe, it truncates the output of the column.

当我尝试使用 to_string 从 a 输出一列时dataframe,它会截断该列的输出。

print gtf_df.ix[:1][['transcript_id','attributes']].to_string(header=False,index=False)

Out: ' CUFF.1.1  gene_id "CUFF.1"; transcript_id "CUFF.1.1"; FPKM '

print gtf_df.ix[:1]['attributes'][0]

Out: 'gene_id "CUFF.1"; transcript_id "CUFF.1.1"; FPKM "1670303.8168650887"; frac "1.000000"; conf_lo "0.000000"; conf_hi "5010911.450595"; cov "9658.694354";'

Any ideas as to how to resolve this problem? Thanks!

有关如何解决此问题的任何想法?谢谢!

回答by Wouter Overmeire

Using __repr__or to_stringcolumns are by default truncated at 50 chars. In versions of Pandas older than 0.13.1, this can be controlled using pandas.set_printoptions():

默认情况下,使用__repr__to_string列被截断为 50 个字符。在 0.13.1 之前的 Pandas 版本中,可以使用pandas.set_printoptions()以下命令进行控制:

In [64]: df
Out[64]:
                                                   A    B
a  this is a very long string, longer than the defau  bar
b                                                foo  baz

In [65]: pandas.set_printoptions(max_colwidth=100)

In [66]: df
Out[66]:
                                                                      A    B
a  this is a very long string, longer than the default max_column width  bar
b                                                                   foo  baz

In more recent versions of Pandas, use this instead:

在最新版本的 Pandas 中,改用这个:

pd.options.display.max_colwidth = 100

回答by neves

Pandas changed how to set this option. Here is the current documentation

Pandas 更改了设置此选项的方式。这是当前的文档

pd.set_option('display.max_colwidth', 100)

See the docs for other nice ones like:

请参阅其他不错的文档,例如:

pd.set_option('display.max_rows', 999)