Python -将数据格式化为使用熊猫的Excel电子表格。

时间:2022-10-09 20:25:12

I want two columns of data the team name and the line. However all my input is just placed into cell B1. (Note that was with the commented out code at the bottom of my snippet). I think I need to iterate through my lists with for loops to get all the teams down the A column, and lines down the B column, but just can't wrap my head around it while doing it with pandas. Any help would be greatly appreciated!

我需要两列数据,团队名称和行。但是我所有的输入都被放入到B1细胞中。(注意在我的代码片段的底部有注释出来的代码)。我认为我需要循环遍历我的列表,让所有的团队沿着A列向下,然后沿着B列行,但是不能在与熊猫一起做的时候绕着它转。如有任何帮助,我们将不胜感激!

Thanks

谢谢

team = []
line = []
# Each row in table find all rows with class name team
for tr in table.find_all("tr", class_="team"):
    # Place all text with identifier 'name' in list named team
    for td in tr.find_all("td", ["name"]):
        team.append(td.text.strip())


for tr in table.find_all("tr", class_="team"):

    for td in tr.find_all("td", ["currentline"]):
        line.append(td.text.strip())
 # Amount of data in lists  
 x = len(team)

# Team name is list team, Line in list line. Create relationship between both data sets
# Creating Excel Document and Placing Team Name in column A, Placing Line in Column B

#Need to add data into Excel somehow
for i in range(0,1):

    for j to line:




""" data = {'Team':[team], 'line' : [line]}

table = pd.DataFrame(data)

writer = pd.ExcelWriter('Scrape.xlsx')
table.to_excel(writer, 'Scrape 1')

writer.save()"""

1 个解决方案

#1


1  

Here you should not do this because you're making lists of lists:

在这里,你不应该这样做,因为你在列清单:

data = {'Team':[team], 'line' : [line]}
# ...

Instead do:

而不是做的事:

data = {'Team': team, 'line': line}
# ...

#1


1  

Here you should not do this because you're making lists of lists:

在这里,你不应该这样做,因为你在列清单:

data = {'Team':[team], 'line' : [line]}
# ...

Instead do:

而不是做的事:

data = {'Team': team, 'line': line}
# ...