I use pandas and openpyxl in order to store data I calculated with Python.
Here an example in which I generate a numpy array, I convert it to a pandas dataframe and then I save it into an excel file located in "path" (remember that if you are working on Windows you need "r" at the beginning of the file path in order to convert the symbol "\" to the symbol "\\")
import numpy as np
import pandas as pd
from openpyxl import load_workbook
path = r"C:\Users\fedel\Desktop\excelData\fileName.xlsx"
data = np.random.randn(100)
data = pd.DataFrame(data)
n = 0
data.to_excel(path, sheet_name = 'sheet number ' + str(n)) # save data in an excel worksheet
Now you can open the "fileName.xlsx" file and see that you stored data on a work sheet named "sheet number 0".
Next step is to generate other data and save them in other worksheets, without deleting the first one
book = load_workbook(path) #load excel file
writer = pd.ExcelWriter(path, engine = 'openpyxl') # use pandas to write in the some excel file
writer.book = book # write what you saved before in order to avoid "overwriting"
for n in range(1, 10):
data = np.random.randn(100)
data = pd.DataFrame(data)
data.to_excel(writer, sheet_name = 'sheet number ' + str(n) ) # iteratively save data on different excel worksheets
writer.save()
writer.close()
When you want to open and analyze the data you stored then I suggest you to type
xls = pd.ExcelFile(path)
df1 = xls.parse(0)
if you want to open data from the first worksheet or
xls = pd.ExcelFile(path)
df1 = xls.parse(1)
if you want to open data from the second one, you may even write
xls = pd.ExcelFile(path)
dataNames = xls.sheet_names
df1 = xls.parse(dataNames[0])