pandas 使用pandas将数据框导出到python中的csv文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51272730/
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
exporting data frame to csv file in python with pandas
提问by An?l Ayd?n
I want to export my dataframe to a csv file. normally I want my dataframe as 2 columns but when I export it, in csv file there is only one column and the data is separated with comma.
我想将我的数据框导出到 csv 文件。通常我希望我的数据框为 2 列,但是当我导出它时,在 csv 文件中只有一列并且数据用逗号分隔。
m is one column and s is another.
m 是一列,s 是另一列。
df = pd.DataFrame({'MSE':[m], 'SSIM': [s]})
to append new data frames I used below function and save data to csv file:.
附加我在下面使用的新数据框并将数据保存到 csv 文件:。
with open('test.csv', 'a+') as f:
df.to_csv(f, header=False)
print(df)
when I print dataframe on console output looks like:
当我在控制台输出上打印数据帧时,如下所示:
MSE SSIM 0 0.743373 0.843658
MSE SSIM 0 0.743373 0.843658
but in csv file a column looks like: here first is index, second is m and last one is s. I want them in 3 seperate columns
但在 csv 文件中,一列看起来像:这里第一个是索引,第二个是 m,最后一个是 s。我希望它们在 3 个单独的列中
0,1.1264238582283046,0.8178900901529639
How can I solve this?
我该如何解决这个问题?
采纳答案by Anton vBR
Your excel setting is most likely ;
(semi-colon). Use:
您的 excel 设置很可能;
(分号)。用:
df.to_csv(f, header=False, sep=';')