'When is a filepath neccessary using pandas when working with an excel file?
I am trying to get into pandas and I am confused here is a program that reads from an excel file and it works without giving it a filepath when the excel file is in the same folder as the python program:
import pandas as pd
excel_file = "RandomExcelNumbers.xlsx"
xldata = pd.read_excel(excel_file, sheet_name= "Sheet1")
xl_Value1 = xldata["ColTitle2"][0]
print(xl_Value1)
Now I tried something different:
import pandas as pd
filename = "Test.xlsx"
def read_value_from_excel(filename, column="C", row=3):
"""Read a single cell value from an Excel file"""
return pd.read_excel(filename, skiprows=row - 1, usecols=column, nrows=1, header=None, names=["Value"]).iloc[0]["Value"]
print(read_value_from_excel(filename, "C", 3))
and it gives me this error message: FileNotFoundError: [Errno 2] No such file or directory: 'Test.xlsx'
Please, what's the difference between these two programs? One needs the filepath, the other does not? Why is that? Is there a way to not neccessarily give the filepath in program no. 2 and let the program compile?
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
