【问题标题】:Print A Pandas Data Frame to a Text File (Python 3)将 Pandas 数据框打印到文本文件(Python 3)
【发布时间】:2014-09-24 22:57:02
【问题描述】:

我有一个像这样的大数据文件

 Words
 One
 Two
 Three
 ....
 Threethousand

我正在尝试使用以下代码将此列表打印到文本文件:

 df1 = df[['Words']]
 with open('atextfile.txt', 'w', encoding='utf-8') as outfile:
         print(df1, file=outfile)

但是发生的情况是它没有打印出整个 DF,它最终看起来像这样:

 Words
 One
 Two
 Three
 ....
 Threethousand
 Fourthousand
 Fivethousand

如何打印出整个 DF?

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    我会使用to_string 来执行此操作,它不像打印那样缩写:

    df['Words'].to_string('atextfile.txt')
    # or
    df[['Words']].to_string('atextfile.txt')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2022-08-17
      • 1970-01-01
      相关资源
      最近更新 更多