1

I have this dataset:

Dataset

That has the column 'Product' with values 'Gas', 'Oil', and 'Water'. I want to write this dataframe to a single excel workbook with three worksheets with the 'Gas', 'Oil', and 'Water' data in those three worksheets.

I have tried:

I have tried variations of:

and keep getting a type error. Any assistance is appreciated.

enter image description here

2
  • naturally you'll get an error, you're appending the entire dataframe to each sheet and Product has NaN values so you're getting the floating point error. Commented Feb 19, 2020 at 4:08
  • can you post next time the data frame directly and not as a picture? That would be helpful. Working on this. You did not defined sheet you want to save the data in in your code Commented Feb 19, 2020 at 4:09

2 Answers 2

3

IIUC,

you can group by product and assign this as the sheet name, whilst assigning the data into the sheet based on the aggregation.

writer = pd.ExcelWriter('Report.xlsx')

for group, data in result2.groupby('Product'):
    data.to_excel(writer,group)
writer.save()
   
Sign up to request clarification or add additional context in comments.

Comments

0

This should work. Try to apply this example to your needs:

import pandas as pd

data = pd.read_excel('test.xlsx', index_col = 0)    

Gas = data.loc['Gas']  # look for index Gas
Oil = data.loc['Oil']  # look for index Oil

writer = pd.ExcelWriter("Test.xlsx", engine = 'xlsxwriter')
Gas.to_excel(writer, sheet_name = 'Gas')
Oil.to_excel(writer, sheet_name = 'Oil')
writer.save()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.