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

标签 python excel pandas beautifulsoup

我想要两列数据:团队名称和线路。然而我的所有输入都只是放入单元格 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/

相关文章:

json - Excel 到 JSON(使用 VBA)土耳其语字符问题

python - Pandas 随机插入不存在的分隔符

python - 根据相似的两列对 pandas 数据框进行排序,但如果另一列具有值,则其中一列将为 NaN

python - 无法从数据帧列表中获取索引位置

python - 连接 Spark Dataframe 中包含列表值的列

python - 如何减少Windows中的CPU使用率?

excel - 在 JOIN 中使用 CASE 加速 SQL 查询

excel - 如何使用 Microsoft Interop for Excel 2003 和 2010

Python 2.6 到 2.5 速查表

python - numba jitclass - 属性 a.x 是 a.x 返回 False