Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

python - pandas to_csv doesn't output the file

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

I tried to use to_csv to 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.

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

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.

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

Update

If I save the file df.to_csv('testfile.csv') then do 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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

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

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...