-3

Data visualisation: I want to import a file using pandas. I assigned a variable and file name given is correct but it shows a error message as file does not exist

Code: sample_data = pd.read_csv('sample_data.csv')

wjandrea
  • 28,235
  • 9
  • 60
  • 81

3 Answers3

0

You're probably in the wrong working directory. Run os.getcwd() to check. If so, you can either change working directories with os.chdir() or give an absolute path to the file instead of a relative path.

If you're already in the right working directory, run os.listdir() to make sure the file is actually there.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

Ok, in this case, what you will have to do is get the path file by right-clicking on the file and going to properties(Windows, don't know about Mac). Then just copy the file path and paste it instead of the file name. So for now, it should be something like this(as I don't know your file path):

sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets')

Now, after the last folder, give in your file name. So now, it should look like this:

sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')

However, this will still not work as the slashes need to be the other way. Because of this, there will have to be an r before the quotes.

sample_data = pd.read_csv(r'C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')

Now this should work as all the requirements are met.

-1

You need to store the .csv file in a folder where the actual program is stored, otherwise you have to give a proper path to the file:

sample_data = pd.read_csv('filepath')
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
  • *"You need to store the .csv file in folder where actual program is store"* - No, the CSV needs to be in the working directory if you give a relative path. *"You Have to give a proper path to the file"* - You mean an absolute path, right? – wjandrea Jun 14 '20 at 15:24