I would like to return each data frame from each URL appended into one single data frame. When I print it within the function, I get the result I desire. The problem is when I try assign a variable to the data frame, it only adds the final data frame. Running this function prints my desired result:
JavaScript
x
12
12
1
import pandas as pd
2
urllist = ['https://basketball.realgm.com/nba/boxscore/2022-04-09/Indiana-at-Philadelphia/388705', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/New-Orleans-at-Memphis/388704', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/Golden-State-at-San-Antonio/388706', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/Sacramento-at-LA-Clippers/388703']
3
def Boxscore(URL):
4
for x in URL:
5
box_list = pd.read_html(x)
6
box1 = box_list[3]
7
box2 = box_list[4]
8
fullbox = pd.concat([box1, box2])
9
print(fullbox)
10
11
Boxscore(urllist)
12
But when I try to assign it a value, it only prints the final data frame, instead of all of them together.
JavaScript
1
13
13
1
import pandas as pd
2
urllist = ['https://basketball.realgm.com/nba/boxscore/2022-04-09/Indiana-at-Philadelphia/388705', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/New-Orleans-at-Memphis/388704', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/Golden-State-at-San-Antonio/388706', 'https://basketball.realgm.com/nba/boxscore/2022-04-09/Sacramento-at-LA-Clippers/388703']
3
def Boxscore(URL):
4
for x in URL:
5
box_list = pd.read_html(x)
6
box1 = box_list[3]
7
box2 = box_list[4]
8
fullbox = pd.concat([box1, box2])
9
return fullbox
10
11
fullboxscore = Boxscore(urllist)
12
print(fullboxscore)
13
How can I append each data frame into one, and name that new data frame as a variable? Please help, thanks!
Advertisement
Answer
Try creating an empty list to append to and then concat
JavaScript
1
14
14
1
def Boxscore(URL: list) -> pd.DataFrame:
2
dfs = [] # empty list
3
for x in URL:
4
box_list = pd.read_html(x)
5
box1 = box_list[3]
6
box2 = box_list[4]
7
fullbox = pd.concat([box1, box2])
8
dfs.append(fullbox) # append frame to list
9
10
return pd.concat(dfs).reset_index(drop=True) # concat frames and return
11
12
# call you function and assign it to a variable
13
fullboxscore = Boxscore(urllist)
14