batrick_pateman
batrick_pateman

Reputation: 29

Combining 2 Dataframes into 1 excel workbook with 2 sheets

I am using the current code to source coronavirus cases and sort into 1 excel workbook with 2 sheets. The code runs without error, but I have no idea where it is? Where would I put my location within my files to get the workbook added to a certain file?

For example, I want the file to be dropped into the file Documents\Coronavirus.

import pandas as pd

url_cases = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
url_deaths = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
df_cases = pd.read_csv(url_cases)
df_deaths = pd.read_csv(url_deaths)

writer = pd.ExcelWriter('Daily Coronavirus Data.xlsx', engine='xlsxwriter')

df_cases.to_excel(writer, sheet_name='Sheet1')
df_deaths.to_excel(writer, sheet_name='Sheet1')

writer.save()

Many thanks in advance.

Upvotes: 1

Views: 72

Answers (3)

Max
Max

Reputation: 13334

If you're trying to create an Excel file with "2 sheets", then why are you writing to the same sheet with sheet_name='Sheet1'? That seems to be the first thing to fix.

As for the path to the Excel file, you didn't specify any (you only provided a file name with 'Daily Coronavirus Data.xlsx'), so the file should be written in the current working directory. 2 ways to go about it:

  • either you find out the working directory: typically where you're initiating your python code from:
import os
print(os.getcwd())
  • or you specify a path: eg pd.ExcelWriter('/my/path/to/Daily Coronavirus Data.xlsx'...

Upvotes: 0

William Baker Morrison
William Baker Morrison

Reputation: 1789

Try this:

writer = pd.ExcelWriter('\Documents\Coronavirus\Daily Coronavirus Data.xlsx', engine='xlsxwriter')

Also, you should avoid whitespace in filenames

Upvotes: 1

Prabhat Kumar Sahu
Prabhat Kumar Sahu

Reputation: 994

It will save in the current directory where you are running the code. I have just changed the name of the file while saving. Run it once and check

import pandas as pd

url_cases = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv' 
url_deaths = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv' 

df_cases = pd.read_csv(url_cases) 
df_deaths = pd.read_csv(url_deaths)

writer = pd.ExcelWriter('Daily_Coronavirus_Data.xlsx', engine='xlsxwriter')

df_cases.to_excel(writer, sheet_name='Sheet1') 
df_deaths.to_excel(writer, sheet_name='Sheet1')

writer.save()

Upvotes: 0

Related Questions