将数据帧保存到 csv 文件(python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49608656/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:10:10  来源:igfitidea点击:

saving a dataframe to csv file (python)

pythonpandascsvdataframe

提问by farhat

I am trying to restructure the way my precipitations' data is being organized in an excel file. To do this, I've written the following code:

我正在尝试重组我的降水数据在 excel 文件中的组织方式。为此,我编写了以下代码:

import pandas as pd

df = pd.read_excel('El Jem_Souassi.xlsx', sheetname=None, header=None)
data=df["El Jem"]

T=[]
for column in range(1,56):
    liste=data[column].tolist()
    for row in range(1,len(liste)):
        liste[row]=str(liste[row])
        if liste[row]!='nan':
            T.append(liste[row])

result=pd.DataFrame(T)
result

This code works fine and through Jupyter I can see that the result is good screenshot

这段代码工作正常,通过 Jupyter 我可以看到结果是好的 截图

However, I am facing a problem when attempting to save this dataframe to a csv file.

但是,在尝试将此数据帧保存到 csv 文件时,我遇到了问题。

 result.to_csv("output.csv")

The resulting file contains the vertical index column and it seems I am unable to call for a specific cell.

结果文件包含垂直索引列,似乎我无法调用特定单元格。

(Hopefully, someone can help me with this problem) Many thanks !!

(希望有人能帮我解决这个问题)非常感谢!!

回答by jjj

It's all in the docs.

这一切都在文档中

You are interested in skipping the index column, so do:

您有兴趣跳过索引列,所以这样做:

result.to_csv("output.csv", index=False)

result.to_csv("output.csv", index=False)

If you also want to skip the header add:

如果您还想跳过标题,请添加:

result.to_csv("output.csv", index=False, header=False)

result.to_csv("output.csv", index=False, header=False)



I don't know how your input data looks like (it is a good idea to make it available in your question). But note that currently you can obtain the same results just by doing:

我不知道您的输入数据是什么样子的(最好在您的问题中提供它)。但请注意,目前您只需执行以下操作即可获得相同的结果:

import pandas as pd
df = pd.DataFrame([0]*16)
df.to_csv('results.csv', index=False, header=False)