Ankit Gupta
Ankit Gupta

Reputation: 13

Why does my code not work? I'm a novice at working with pandas on Python3

I'm using Google colab to write code and been trying to import an excel file (.xlsx) onto it using pandas

import pandas as pd

df = pd.ExcelFile('‪C:/Users/Ankit Gupta/Downloads/DS1.xlsx')
df.head()

Error:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-9-a8b5ec91cbb7> in <module>()
      1 import pandas as pd
      2 
----> 3 df = pd.ExcelFile('‪C:/Users/Ankit Gupta/Downloads/DS1.xlsx')
      4 df.head()

This keeps showing. My path is correct, and I'm new to working with pandas. Can anyone help?

Upvotes: 1

Views: 58

Answers (4)

A_B
A_B

Reputation: 29

When I started working with pandas, even I used to face this problem. So here are the few check up you should do:

1.) Spelling errors and case sensitive nature

2.) Location. I would suggest as a beginner make a folder in C drive users, wherever your anaconda files are stored. And in that particular folder save all your datasets. I am using this method and till date never found the error again.

3.) While writing name, mention it along with your file format.

Solution to your question:

SOL1:

Possibly the path is different or changed, so Open Anaconda prompt or cmd, then change the path. Lets assume you are in "c" drive and file is in 'd' drive. So open the cmd and write "d:" and hit enter. Then you will se "D:>". Now you should write "cd. After running that, write "jupyter notebook" and run it. It will generate and open a new notebook in the same folder that has your file.

SOL2:

Other Solution is using backslash, two backslashes.The problem is with using backslashes "". You must avoid that. Backslash is reserved for something called escape characters, like new line being denoted with "\n" and stuff. You should use two backslashes "\" or one forward slash "/"/

Upvotes: 0

Sanket Singh
Sanket Singh

Reputation: 1366

Use:

df = pd.ExcelFile('‪C:\Users\Ankit Gupta\Downloads\DS1.xlsx')

Basically "\" instead of "/" .

Upvotes: 0

Igor Rivin
Igor Rivin

Reputation: 4864

This has nothing to do with pandas. You are using google.colab, which is in the cloud and does not see your local files. Use from google.colab import files followed by files.upload().

Upvotes: 1

ForceBru
ForceBru

Reputation: 44838

r'‪C:\\Users\\Ankit Gupta\\Downloads\\DS1.xlsx should've been r'‪C:\Users\Ankit Gupta\Downloads\DS1.xlsx. Your current path has two backslashes (r'\\') instead of one.

Upvotes: 1

Related Questions