简体   繁体   中英

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?

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.

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

Use:

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

Basically "\" instead of "/".

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 "/"/

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