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 = {
        '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)

# 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)

# 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

........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.. :)



Jun 3, 2020 in Python by Edureka
• 120 points

edited Jun 5, 2020 by Gitika 1,895 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
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 4, 2018 in Python by Omkar
• 69,090 points

edited Aug 8, 2018 by Omkar 5,285 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,910 points
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 6, 2020 in Python by Gitika
• 65,870 points
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
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 6, 2019 in Python by Neha
• 330 points

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