【问题标题】:pandas DataFrame.to_string() truncating strings from columnspandas DataFrame.to_string() 从列中截断字符串
【发布时间】:2012-08-16 20:11:51
【问题描述】:

当我尝试使用 to_string 从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";'

关于如何解决这个问题的任何想法? 谢谢!

【问题讨论】:

  • 属性的类型是什么 -- 即你从 type(gtf_df['attributes'][0]) 得到什么 -- 如果是字符串,能否告诉我们是否删除引号和分号解决了问题——即 gtf_df.attributes = gtf_df.attributes.replace('\"','').replace(';','')
  • 它是一个字符串列。但是,您推荐的修复并没有改变任何东西。下面给出的答案有效。

标签: python pandas


【解决方案1】:

使用 __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

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

pd.options.display.max_colwidth = 100

【讨论】:

  • 谢谢。这绝对是问题所在。
  • 截至目前,Pandas 版本为 0.13.1,设置格式为pd.options.display.max_colwidth = 100
【解决方案2】:

Pandas 更改了如何设置此选项。这是current documentation

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

查看文档以了解其他不错的文档,例如:

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

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2020-10-21
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多