pandas to_csv 不输出文件

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

pandas to_csv doesn't output the file

pythonpandas

提问by Li Ai

I'm using jupyter notebook pandas to_csvdoesn't output the dataframe to a file.

我正在使用 jupyter notebook pandasto_csv不会将数据帧输出到文件。

I tried to use to_csvto output a dataframe to a csv file by setting the working directory or specify the directory, and it didn't create any file. The code ran and didn't produce any error message, but when I opened the folder, there was no such file.

我试图to_csv通过设置工作目录或指定目录来使用将数据帧输出到 csv 文件,但它没有创建任何文件。代码运行并没有产生任何错误消息,但是当我打开文件夹时,没有这样的文件。

I tried a different IO, and it did show the result had been output.

我尝试了不同的 IO,它确实显示结果已输出。

from io import StringIO

output = StringIO()

a.to_csv(output)

print(output.getvalue())

I got the following output:

我得到以下输出:

,a

0,1

1,2

2,3

but again to_csv('filepath/filename.csv')doesn't output any file.

但同样to_csv('filepath/filename.csv')不输出任何文件。

PS: I can read any file in any directory using read_csv().

PS:我可以使用 read_csv() 读取任何目录中的任何文件。

Update

更新

If I save the file df.to_csv('testfile.csv')then do pd.read_csv('testfile.csv')

如果我保存文件df.to_csv('testfile.csv')然后做pd.read_csv('testfile.csv')

I can read the file but cannot see it in the directory.

我可以读取文件,但在目录中看不到它。

Also, doing [x for x in os.listdir() if x == 'testfile.csv']will list the file.

此外,这样做[x for x in os.listdir() if x == 'testfile.csv']会列出文件。

回答by Matt Moehr

I think the issue is that you're running a Jupyter Notebook so the "current directory" for the notebook is probably somewhere in "C:\Users\user_name\AppData...".

我认为问题在于您正在运行 Jupyter Notebook,因此笔记本的“当前目录”可能位于“C:\Users\user_name\AppData...”中。

Try running os.getcwd()on its own in your notebook. It probably won't be the same folder as where the *.ipynbfile is saved. So as @Chris suggested in comments, this:

尝试os.getcwd()在您的笔记本中自行运行。它可能与*.ipynb保存文件的文件夹不同。所以正如@Chris 在评论中建议的那样:

df.to_csv(os.getcwd()+'\\file.csv')

df.to_csv(os.getcwd()+'\\file.csv')

... will send your csv into the AppData folder.

... 将您的 csv 发送到 AppData 文件夹中。

You could either change the working directory for the Jupyter notebook, or you could use a fully specified filename like:

您可以更改 Jupyter notebook 的工作目录,也可以使用完全指定的文件名,例如:

df.to_csv('C:\\Users\\<user_name>\\Desktop\\file.csv')

df.to_csv('C:\\Users\\<user_name>\\Desktop\\file.csv')

(Note: this also tripped me up in VS-Code while using the interactive iPython execution that happens when you press shift+enter. Interestingly, in VS-Code, if you use ctrl+shift+p and select "Python: Run selection..." it executes in your default terminal, which doesn't have this problem.)

(注意:在使用按 shift+enter 时发生的交互式 iPython 执行时,这也让我在 VS-Code 中绊倒了。有趣的是,在 VS-Code 中,如果您使用 ctrl+shift+p 并选择“Python:运行选择。 ..”它在您的默认终端中执行,它没有这个问题。)

回答by user12352611

I had the same problem using spyder. In my case it was caused by the internet security tool (COMODO) I used, which somehow executed spyder in a sandbox or so and not allowed it to write to the "normal" directories. Instead it saved the result to a folder named C:VTRoot\HarddiskVolume2\users\. You can try to save a file with a quite unique name a.to_csv('very_unique_filename.csv') and then search in the windows explorer for that filename to find the folder it is stored in. If the reason is some tool like this, changing it's settings may help.

我在使用 spyder 时遇到了同样的问题。在我的情况下,它是由我使用的互联网安全工具 (COMODO) 引起的,它以某种方式在沙箱中执行 spyder 并且不允许它写入“正常”目录。相反,它将结果保存到名为 C:VTRoot\HarddiskVolume2\users\ 的文件夹中。您可以尝试使用非常独特的名称 a.to_csv('very_unique_filename.csv') 保存文件,然后在 Windows 资源管理器中搜索该文件名以找到存储它的文件夹。如果原因是这样的工具,更改它的设置可能会有所帮助。

回答by Chris

you probably forgot to add the name of the file after your path, so it will named your file as the last character of your path, which you can see on the home page of jupyter.

您可能忘记在路径后添加文件名,因此它会将您的文件命名为路径的最后一个字符,您可以在 jupyter 的主页上看到。

should be: df.to_csv('path/filename.csv', ....)

应该是:df.to_csv('path/filename.csv', ....)

rather than df.to_csv('path.csv'......)

而不是 df.to_csv('path.csv'......)

回答by jcyan

Maybe you do not have access to your output folder.

也许您无权访问输出文件夹。

First try the current dir, like to_csv('tmp.csv').

首先尝试当前目录,例如to_csv('tmp.csv').

Then check the directory's ownership by using ls -l.

然后使用ls -l.