Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python-pandas: .to_csv() not creating the *.csv file

(delete after answered – I can’t find a similar issue in SO database but feel the problem is something silly)

Edit: It is created, but only shows through windows explorer (Pycharm doesn’t update the new file in the cwd).

The *.csv file isn’t showing up in the working directory. I’m using Pycharm and have installed the pandas package and have the "ExcelReader" plugin.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas

data = pandas.read_csv("2018_Central_Park_Squirrel_Census_-_Squirrel_Data.csv")
gray = sum(data["Primary Fur Color"] == "Gray")
red = sum(data["Primary Fur Color"] == "Cinnamon")
black = sum(data["Primary Fur Color"] == "Black")
squirrel_dict = {
    "Color": ["Gray", "Red", "Black"],
    "Population": [gray, red, black],
}
squirrels_by_color = pandas.DataFrame(squirrel_dict)
print(squirrels_by_color)

#    Color  Population
# 0   Gray        2473
# 1    Red         392
# 2  Black         103

squirrels_by_color.to_csv("squirrels_by_color.csv")
# not creating the *.csv file ????????
# I've installed pandas and "ExcelReader" plugin...

In the tutorial video the file will appear in the working directory after using the *.to_csv() command and launching the script. I’m 90% sure my code is correct (the dataframe looks good), yet it doesn’t create the file.

>Solution :

It is hard to say exactly what the issue is without knowing more info. But if you run (in a jupyter cell):

import os
os.getcwd()

It will print your current working directory (cwd). This is where your file would be saving, since you have specified a relative path that says to save the file in your cwd. If you are not in a jupyter notebook, and are running your python file in the terminal, the file will save at the specified location when you run pwd in the terminal (or cd if you are on windows). Your code does look good to me also, and even if squirrels_by_color was an empty df, it would still save an empty .csv in your current working directory.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading