Hi, I am very new to Python but doing my best to learn it. I am still making some very fundamental mistakes as you can probably tell. Also, how do people display their code properly on this forum? The only way I seem to be able to do it is this way which seems a lot more dry.
My question is why do I keep getting the associated error with the latter part of this script (the writing to excel spreadsheet part)?
Here is my script, commented in the hope I may make my thought process simpler to provide feedback on..
# import necessary libraries for the complete script (more later)
import sys
from SALib.sample import saltelli
from SALib.analyze import sobol
from SALib.analyze import morris
import numpy as np
import pandas as pd
# set number of parameters and ranges to generate the random values from.
problem = {
'num_vars':9,
'names':['qadsconwwstream', 'kadsconwwstream', 'muhconwwstream', 'khssconwwstream', 'bhconwwstream', 'khconwwstream', 'khxsconwwstream', 'difreswwstream', 'yhairconwwstream'],
'bounds': [[0, 0.5],
[0, 0.25],
[0, 16],
[0, 25],
[0, 3.1],
[0, 15],
[0, 0.5],
[0, 2.5],
[0, 3.33]]
}
# generate random values within the range and assign number of iterations
dfBOD = saltelli.sample(problem, 10)
# create the 1st row in excel, the column header names (because for some reason the 'names' in problem section above haven't been written)
new_row = pd.DataFrame({'qadsconwwstream', 'kadsconwwstream', 'muhconwwstream', 'khssconwwstream', 'bhconwwstream', 'khconwwstream', 'khxsconwwstream', 'difreswwstream', 'yhairconwwstream'})
dfBOD = pd.concat([new_row, dfBOD]).reset_index(drop = true)
dfBOD.head(10)
# create the 2nd row, the units for the headers in first row.
new_row = pd.DataFrame({'1/(gCOD/m3)/d', '-', '1/d', 'mgCOD/L', '1/d', '1/d', '-', '-', 'gCOD/gCOD'})
dfBOD = pd.concat([new_row, dfBOD]).reset_index(drop = true)
dfBOD.head(11)
# Write the df with the headers and generated values to an excel file
#create a Pandas Excel writer object using xlsxwriter as the engine.
writer = pd.ExcelWriter('BOD_inputvalues200.xlsx',
engine = 'xlsxwriter')
# write the dataframe to the worksheet
dfBOD.to_excel(writer, sheet_name = 'sheet1')
#close the pandas excel writer object and output the excel file
writer.save()
........and here is the associated error..
dfBOD.to_excel(writer, sheet_name = 'sheet1')
AttributeError: 'numpy.ndarray' object has no attribute 'to_excel'
I'm sure this is a very simple fault but any advice from the experts out there would be much appreciated.. :)
Thanks,
Dave