How to write df to excel without attribute error, and how to add 2 header columns (1 header, 1 units)

0 votes

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

Jun 3 in Python by Edureka
• 120 points

edited Jun 5 by Gitika 116 views

Hi, @There,

First, note that to_excel is a method on DataFrame, so it fails when invoked on a np.ndarray.

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 69,040 points

edited Aug 8, 2018 by Omkar 3,623 views
0 votes
1 answer

How to have values from a List1 into Columns Names and List 2 Values as Index for an Empty DataFrame

Try like this, it will give you ...READ MORE

answered Jul 10, 2019 in Python by Cherukuri
• 32,460 points
174 views
0 votes
0 answers

How to mix read() and write() on Python files in Windows

It appears that a write() immediately following a read() on a ...READ MORE

Oct 23, 2018 in Python by Aryya
• 500 points
113 views
0 votes
1 answer

How can I write code to find a palindrome in python without using string functions?

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)): In ...READ MORE

answered Jan 16, 2019 in Python by charlie_brown
• 7,770 points
6,841 views
0 votes
0 answers

how to add a header to a python request module?

can you give the syntax and an ...READ MORE

May 7, 2019 in Python by Waseem
• 4,530 points
30 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,060 views
0 votes
0 answers