Skip to content
Advertisement

Read excel file in python using pandas

I am trying to read excel file in pycharm using pandas. I installed the package successfully. My issue is that I am trying to use file location in addition to its name I tried many thing as follow:

import pandas as pd
fileLocation = "C:\Users\GTS\Desktop\Network Interdiction Problem\Manuscript\Interdiction_Data.xlsx"
fileName = 'Interdiction_Data.xlsx'
data = pd.read_excel('fileLocation'+'fileName')

However I keep receiving the following error

Traceback (most recent call last):
  File "C:/Users/GTS/PycharmProjects/Reliability1/Reliability1.py", line 4, in <module>
    data = pd.read_excel('fileLocation'+'fileName')
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 824, in __init__
    self._reader = self._engines[engine](self._io)
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_xlrd.py", line 21, in __init__
    super().__init__(filepath_or_buffer)
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagespandasioexcel_xlrd.py", line 36, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "C:UsersGTSPycharmProjectsReliability1venvlibsite-packagesxlrd__init__.py", line 111, in open_workbook
    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'fileLocationfileName'

Any Idea?

Thanks in advance

Advertisement

Answer

Your fileLocation variable includes the name of the file. reading fileLocation + fileName is essentially reading

C:\Users\GTS\Desktop\Network Interdiction Problem\Manuscript\Interdiction_Data.xlsxInterdiction_Data.xlsx

Another issue is that you have quotation marks around your variable names when calling pd.read_excel() meaning that you are passing a string to the function.

Try:

data = pd.read_excel(fileLocation)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement