Skip to content

Instantly share code, notes, and snippets.

@mathcass
Created May 31, 2018 21:46
Show Gist options
  • Save mathcass/1ec4ccd48fccffb89dce76094086c658 to your computer and use it in GitHub Desktop.
Save mathcass/1ec4ccd48fccffb89dce76094086c658 to your computer and use it in GitHub Desktop.
Demonstrates how to combine multiple pd.DataFrame objects into an Excel sheet
"""Demonstrate how to save pd.DataFrame objects to named Excel sheets
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html
Requires having openpyxl installed for writing to Excel documents
"""
import pandas as pd
df1 = pd.DataFrame({
'names': ['Alvin', 'Simon', 'Theodore'],
'colors': ['Red', 'Blue', 'Yellow']
})
df2 = pd.DataFrame({
'Cities': ['Seattle', 'Atlanta', 'Freetown'],
'Temperature': [60, 85, 95]
})
with pd.ExcelWriter('output.xlsx') as writer:
df1.to_excel(writer, 'Chimpmonks', index=False)
df2.to_excel(writer, 'Cities', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment