gpt4 book ai didi

Python - 使用 pandas 将数据格式化为 Excel 电子表格

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:27 25 4
gpt4 key购买 nike

我想要两列数据:团队名称和线路。然而我的所有输入都只是放入单元格 B1 中。 (请注意,这是我的代码片段底部注释掉的代码)。我想我需要用 for 循环迭代我的列表,以使所有团队都沿着 A 列,并沿着 B 列,但在使用 pandas 执行此操作时,我无法理解它。任何帮助将不胜感激!

谢谢

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()"""

最佳答案

这里你不应该这样做,因为你正在制作列表列表:

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

而是这样做:

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

关于Python - 使用 pandas 将数据格式化为 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com