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, 2020 in Python by Edureka
• 120 points

edited Jun 5, 2020 by Gitika 9,622 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

–1 vote
2 answers
+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 5, 2018 in Python by Omkar
• 69,210 points

edited Aug 8, 2018 by Omkar 7,678 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
• 33,030 points
776 views
0 votes
1 answer

How to write a code In Python where input will be 1 then the output will be 2?

You can go through this:  def num(number): ...READ MORE

answered Oct 7, 2020 in Python by Gitika
• 65,910 points
936 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 24, 2018 in Python by Aryya
• 500 points
634 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,072 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP