将 Pandas 数据帧写入 .txt 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51829923/
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
write a Pandas dataframe to a .txt file
提问by Alejandro Rodriguez
My code writes a txt file with lots of data. I'm trying to print a pandas dataframe into that txt file as part of my code but can't use .write() as that only accepts strings.
我的代码编写了一个包含大量数据的 txt 文件。我正在尝试将 Pandas 数据帧作为代码的一部分打印到该 txt 文件中,但不能使用 .write() 因为它只接受字符串。
How do I take a pandas dataframe, stored as DF1 for example, and print it in the file?
如何获取存储为 DF1 的 Pandas 数据帧,并将其打印在文件中?
I've seen similar questions but those are aimed at creating a txt file solely for the dataframe, I would just like my dataframe to appear in a txt file
我见过类似的问题,但这些问题的目的是为数据框创建一个 txt 文件,我只是希望我的数据框出现在 txt 文件中
回答by ALollz
use the to_string
method, and then you can use write
with the mode set to append
('a'
)
使用该to_string
方法,然后您可以write
在模式设置为append
( 'a'
) 的情况下使用
tfile = open('test.txt', 'a')
tfile.write(df.to_string())
tfile.close()
Sample Data
样本数据
import pandas as pd
import numpy as np
df = pd.DataFrame({'id': np.arange(1,6,1),
'val': list('ABCDE')})
test.txt
测试.txt
This line of text was here before.
Code
代码
tfile = open('test.txt', 'a')
tfile.write(df.to_string())
tfile.close()
Output: test.txt
输出:test.txt
This line of text was here before.
id val
0 1 A
1 2 B
2 3 C
3 4 D
4 5 E
回答by firxworx
Pandas DataFrames have to_string()
, to_json()
and to_csv()
methods that may be helpful to you, see:
PandasDataFrames有to_string()
,to_json()
而且to_csv()
可能对你有帮助的方法,请参阅:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_string.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_string.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
Example of writing a text file to a string. Use 'w' flag to write and 'a' to append to a file.
将文本文件写入字符串的示例。使用 'w' 标志写入并使用 'a' 附加到文件。
example_string = df1.to_string()
output_file = open('file.txt','a')
output_file.write(example_string)
output_file.close()
If you are only looking to put certain information in the text file, you can do that using either pandas or json methods to select it, etc. and see the docs links above as well.
如果您只想将某些信息放入文本文件中,您可以使用 Pandas 或 json 方法来选择它等,并查看上面的文档链接。
Before OP commented about appending I originally wrote an example about json. json
supports a dump()
method to help write to a file. However, in most cases, its not the most ideal format to keep appending output to vs. csv or txt. In case its useful to anyone:
在 OP 评论附加之前,我最初写了一个关于 json 的例子。json
支持一种dump()
帮助写入文件的方法。但是,在大多数情况下,将输出附加到 csv 或 txt 并不是最理想的格式。如果它对任何人有用:
import json
filename = 'file.json'
with open(filename, 'w') as file:
json.dump(df1.to_json(), file)