pandas 在python中将输出另存为数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47577284/
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
Save output as a dataframe in python
提问by Andriana
I'm running a script in python and I'm interested in two of the outputs that are calculated in the script. They are both arrays. What I want to do is save these arrays every time I run the script in order to keep track of the results. Then I might need to use all these saved variables in a different script that makes some kind of comparison between the variables. In general I would like to be able to use these variables every time I want to and perform some kind of analysis of the values in the arrays. Thus, I was wondering if there is any way to save these two arrays as dataframes and then import them with pandas in my script. Or is there a different way that you would recommend?
我在 python 中运行一个脚本,我对脚本中计算的两个输出感兴趣。它们都是数组。我想要做的是每次运行脚本时保存这些数组,以便跟踪结果。然后我可能需要在不同的脚本中使用所有这些保存的变量,以便在变量之间进行某种比较。一般来说,我希望每次都能够使用这些变量并对数组中的值进行某种分析。因此,我想知道是否有任何方法可以将这两个数组保存为数据帧,然后在我的脚本中使用 pandas 导入它们。或者你有什么不同的方式推荐?
回答by manandearth
You can create a dataframe from a dict of equal length lists or Numpy arrays:
您可以从等长列表或 Numpy 数组的 dict 创建数据框:
data = { 'character' : [ 'Pooh', 'Eeore', 'Rabbit', 'Piglet'], 'age' : [5, 10, 7, 3], 'colour' : [ 'Yellow', 'Grey', 'Brown', 'Pink'] }
frame = pd.DataFrame(data)
to write out use DataFrame to_csv method:
写出使用 DataFrame to_csv 方法:
data.to_csv('YOUR_FILE/HERE.csv')
回答by Andriana
I use the following code to export data. This will save your dataframe as a text file with the columns separated by tabs.
我使用以下代码导出数据。这会将您的数据框保存为文本文件,其中的列由制表符分隔。
expData = pd.DataFrame(data, columns = ['name1','name2',...,'nameN'])
expData.to_csv("file_%02d.txt" %loopIndex, sep = '\t')
The pd stands for pandas, which I imported as pd
pd 代表Pandas,我将其导入为 pd
import pandas as pd
The loop index will indicate which input you wrote away (input from loop 1, 2 ... n). This will yield an output denoted as file_01.txt, file_02.txt... .
循环索引将指示您写掉的输入(来自循环 1、2 ... n 的输入)。这将产生一个输出,表示为 file_01.txt, file_02.txt... 。
You will need csv to export the data, so install and
您将需要 csv 来导出数据,因此请安装并
import csv
To read this data, just use:
要读取此数据,只需使用:
with open("file.txt", 'r') as f:
reader = csv.reader(f, dialect = 'excel', delimiter = '\t')
for row in reader:
% do something
Hope this is useful to you!
希望这对你有用!