简体   繁体   中英

Why does python not recognise this as my dataset even though the file clearly exists on my computer?

I am a beginner at python and i'm trying to improve my skills by trying to follow this python tutorial: https://towardsdatascience.com/exploratory-analysis-python-kaggle-data-b0afb6ec1788

The line i typed in was:

df = pd.read_csv('C:\Stuff\Python\Medical Appointment Data\792_3538_bundle_archive.zip\KaggleV2-May-2016.csv')

I think the line is meant to enable python to read the data so we can later construct a few graphs. But when i type it in it gives me quite a long error here:

https://gyazo.com/28f7c49e70d3e968cdeb6fd653b4ea04 (sorry i didn't want to take up too much space including it all here)

I have tried a couple things including moving the file i'm trying to get it to use and also some formatting things like changing what quotes i use and the brackets.

I found this page which i think references this issue but haven't found any working solution from there.

"CSV file does not exist" for a filename with embedded quotes

This is my full code so far:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
import plotly.plotly as pyimport plotly.offline as pyo
import plotly.graph_objs as go
pyo.offline.init_notebook_mode()
df = pd.read_csv('C:\Stuff\Python\Medical Appointment Data\792_3538_bundle_archive.zip\KaggleV2-May-2016.csv')

Thank you in advance for any and all help recieved. Let me know if you need any extra information. Also i'm sorry if this is layout is bad or improper. This is my first question and i tried my best. (I can also try fix it if necessary)

The adress contains a lot of escape characters.It should be read as an raw string.Try putting an r infornt of the string like this.

 df = pd.read_csv(r'C:\Stuff\Python\Medical Appointment Data\792_3538_bundle_archive.zip\KaggleV2-May-2016.csv')

Alternatively you can put the data file in same folder as the program file,then you don't need to mention the path.

You can either use a raw string:

df = pd.read_csv(r'C:\Stuff\Python\Medical Appointment Data\792_3538_bundle_archive.zip\KaggleV2-May-2016.csv')

Or escape all the slashes there:

df = pd.read_csv('C:\\Stuff\\Python\\Medical Appointment Data\\792_3538_bundle_archive.zip\\KaggleV2-May-2016.csv')

I highly recommend to use raw strings and put all your data files to the dedicated directory in your project, then your path will be shorter and more readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM