Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Python pandas excel import NameError

I am trying to import data from Excel into Python via Pandas on Mac. When I apply the following code:

import pandas as pd
df = pd.read_excel('/Users/andreasmelsen/Desktop/Data.xlsx', sheetname='Sheet1')
print(df)

I receive the following error message:

NameError: name 'df' is not defined

Am I missing something? I have no issues importing Pandas.

Edit: I am using Visual Studio Code v1.52.1 with Python 3.9.0 64 bit)

question from:https://stackoverflow.com/questions/65829484/python-pandas-excel-import-nameerror

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try:

df = pd.read_excel(r'/Users/andreasmelsen/Desktop/Data.xlsx', sheet_name='Sheet1')

I have added r before the path & corrected the variable name of sheet_name


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...