Home > Blockchain >  Pandas Only Exporting 1 Table to Excel but Printing all
Pandas Only Exporting 1 Table to Excel but Printing all

Time:10-21

The code below only exports the last table on the page to excel, but when I run the print function, it will print all of them. Is there an issue with my code causing not to export all data to excel?

I've also tried exporting as .csv file with no luck.

import pandas as pd

url = 'https://www.vegasinsider.com/college-football/matchups/'

dfs = pd.read_html(url)
for df in dfs:
    if len(df.columns) > 1: 
        df.to_excel(r'VegasInsiderCFB.xlsx', index = False)
        #print(df)

CodePudding user response:

Your problem is that each time df.to_excel is called, you are overwriting the file, so only the last df will be left. What you need to do is use a writer and specify a sheet name for each separate df e.g:

url = 'https://www.vegasinsider.com/college-football/matchups/'
writer = pd.ExcelWriter('VegasInsiderCFB.xlsx', engine='xlsxwriter')
dfs = pd.read_html(url)
counter = 0
for df in dfs:
    if len(df.columns) > 4:
        counter  = 1
        df.to_excel(writer, sheet_name = f"sheet_{counter}", index = False)
writer.save()

You might need pip install xlsxwriter xlwt to make it work.

Exporting to a csv will never work, since a csv is a single data table (like a single sheet in excel), so in that case you would need to use a new csv for each df.

As pointed out in the comments, it would be possible to write the data onto a single sheet without changing the dfs, but it is likely much better to merge them:

import pandas as pd
import numpy as np
url = 'https://www.vegasinsider.com/college-football/matchups/'
dfs = pd.read_html(url)

dfs = [df for df in dfs if len(df.columns) > 4]
columns = ["gameid","game time", "team"]   list(dfs[0].iloc[1])[1:]
N = len(dfs)
values = np.empty((2*N,len(columns)),dtype=np.object)
for i,df in enumerate(dfs):
    time = df.iloc[0,0].replace(" Game Time","")
    values[2*i:2*i 2,2:] = df.iloc[2:,:]
    values[2*i:2*i 2,:2] = np.array([[i,time],[i,time]])
newdf = pd.DataFrame(values,columns = columns)

newdf.to_excel("output.xlsx",index = False)

I used a numpy.array of object type to be able to copy a submatrix from the original dataframes easily into their intended place. I also needed to create a gameid, that connects the games across rows. It should be now trivial to rewrite this so you loop through a list of urls and write these to separate sheets.

  • Related