【问题标题】:Python - Formatting Data into Excel Spreadsheet Using pandasPython - 使用 pandas 将数据格式化为 Excel 电子表格
【发布时间】:2016-07-27 22:03:22
【问题描述】:

我想要两列数据,团队名称和行。然而,我所有的输入都只是放在单元格 B1 中。 (请注意,这是在我的 sn-p 底部注释掉的代码)。我认为我需要使用 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()"""

【问题讨论】:

  • 您注释掉的代码会在我的测试中产生您希望的结果。
  • 它将我所有的结果放入单元格 B1 我希望团队在一列中,而行在另一列中。那有意义吗? @伯尼
  • 有道理我只是说在我的测试中输出如你所愿。
  • 嗯,这真的很奇怪,知道为什么会这样,你有哪一年的优秀成绩? @伯尼
  • 我现在是 2013 年。不知道你的情况出了什么问题......

标签: python excel pandas beautifulsoup


【解决方案1】:

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

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

改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2017-10-24
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多