Home > Mobile >  Is there some way to call and store an excel file for a user defined function? [duplicate]
Is there some way to call and store an excel file for a user defined function? [duplicate]

Time:09-17

I have written a python program to find and interpolate values for 4 variables as inputs from a an excel file.

I have to run this function in loop more than 1000 times. So, each time I call the function in loop it has to open and read this excel file. Is there a faster way solve this such that the excel file can be stored within the function as pandas dataframe or numpy array and the excel file doesn't have to be called each time whilst iteration?

CodePudding user response:

Your coding knowledge will increase a lot just by googling your question and reading documentation/other Stack Overflow posts that already exist.

To answer your question, you can read your entire CSV at once and save it as a variable to use for the rest of your code. From the pandas docs (https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html or https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html)

They show

pd.read_csv('data.csv') 

or

pd.read_excel('data.xlsx')

CodePudding user response:

You can do that. To read data into pandas dataframe you a use:

df = pd.read_excel('your_input_file.xlsx')

To write the dataframe back to an excel file after processing you can use:

df.to_excel("your_output_file.xlsx")  
  • Related