使用df-to-excel时,在多个页面上不断出现错误,在1页上工作正常

2024-09-30 22:11:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码一切都很好,直到我尝试和发送到excel。我有一个脚本,可以很好地为一个网页,但不是多个网页。你知道吗

工作代码和我想要的:

import pandas as pd
from pandas import ExcelWriter


dfs = pd.read_html('https://www.teamrankings.com/nfl/stat/yards-per-play/',header=0)
for df in dfs:
    print(df)

writer = pd.ExcelWriter('nfl.xlsx')


df.to_excel('nflypp.xlsx', sheet_name='yppo', index=False, engine='xlsxwriter')

writer.save()

无工作代码:

import pandas as pd
from pandas import ExcelWriter


oyyp_df = pd.read_html('https://www.teamrankings.com/nfl/stat/yards-per-play.html',header=0)
dyyp_df = pd.read_html('https://www.teamrankings.com/nfl/stat/opponent-yards-per-play',header=0)
for df in (oyyp_df, dyyp_df):
    print(df)


writer = pd.ExcelWriter('nfl.xlsx') 


df.to_excel('nflypp.xlsx', sheet_name='yppo', index=False, engine='xlsxwriter')
df.to_excel('nflypp.xlsx', sheet_name='yppd', index=False, engine='xlsxwriter')


writer.save()

一直工作到最后一刻数据框到excel你知道吗

error: AttributeError: 'list' object has no attribute 'to_excel'

这是输出

C:\Cabs\projects>;nflstatsypp.py公司
[排名球队2018年最后3名最后1名2017年主客场
0 1堪萨斯城7.0 7.0 6.9 6.4 7.5 6.1
1 2 LA充电器6.8 6.4 6.2 6.6 6.9 5.9
2 3 LA闸板6.7 6.2 5.4 7.0 6.4 5.8
3 4坦帕湾6.5 6.3 5.3 6.3 6.8 5.6
4 5新奥尔良6.2 6.0 3.6 6.7 5.7 6.3
5 6匹兹堡6.2 6.0 5.3 6.2 6.2 5.8
6.2 7.3 6.8 6.1 6.2 5.1
7 8亚特兰大6.0 5.0 2.9 6.5 5.5 5.8
8 9绿湾6.0 5.4 4.4 5.9 6.1 4.9
9 10丹佛5.9 6.1 6.3 6.1 5.8 4.8
10 11新英格兰5.9 6.2 6.6 6.2 5.5 6.0
11 12纽约巨人5.8 6.2 5.0 5.4 6.1 4.9
12 13休斯顿5.7 6.0 5.2 6.2 5.3 5.0
13 14西雅图5.7 6.2 6.8 5.5 5.9 5.2
14 15旧金山5.7 5.8 6.1 5.4 5.9 5.3
15 16印第安纳波利斯5.7 5.7 3.7 6.2 5.1 4.6
16 17辛辛那提5.6 5.1 4.8 5.5 5.7 4.8
17 18明尼苏达州5.6 5.1 4.7 5.6 5.6 5.4
18 19奥克兰5.5 5.3 6.4 6.2 5.0 5.4
19 20费城5.5 5.4 6.1 5.5 5.5 5.6
20 21芝加哥5.5 4.6 4.9 6.0 5.0 4.9
21 22克利夫兰5.4 7.3 8.2 5.1 5.8 4.9
22 23田纳西州5.4 7.1 7.5 5.8 5.0 5.2
23 24迈阿密5.4 4.7 3.5 5.8 4.9 4.9
24 25达拉斯5.3 5.2 4.7 5.6 5.1 5.3
25 26底特律5.3 5.0 4.8 5.2 5.5 5.5
26 27巴尔的摩5.2 5.4 4.8 5.3 5.2 4.6
27 28华盛顿5.2 4.8 5.6 5.0 5.4 5.3
28 29杰克逊维尔5.0 4.3 3.8 5.0 5.1 5.4
29 30 NY喷气式飞机4.9 4.5 4.3 5.4 4.4 5.0
30 31水牛4.5 6.2 6.3 4.5 4.6 4.7
31 32亚利桑那州4.4 4.8 5.5 4.5 4.2 4.7]
[排名球队2018年最后3名最后1名2017年主客场
巴尔的摩4.6 4.1 2.9 4.5 4.8 5.0
1 2水牛4.9 4.2 3.5 5.1 4.7 5.3
2 3芝加哥4.9 4.8 5.0 4.6 5.2 5.1
3 4匹兹堡5.2 5.1 6.2 5.6 4.8 5.3
4 5达拉斯5.3 5.2 3.6 4.9 5.6 5.1
明尼苏达州5.3 5.4 6.6 4.6 5.9 4.8
亚利桑那州5.3 5.1 4.4 5.0 5.6 4.9
7 8杰克逊维尔5.3 5.6 7.5 4.3 6.2 4.8
8 9休斯顿5.4 6.1 8.2 5.9 4.9 5.7
9 10田纳西州5.4 5.1 3.8 5.0 5.7 5.1
10 11 LA充电器5.5 5.1 5.3 5.7 5.4 5.3
11 12印第安纳波利斯5.5 4.8 3.9 5.6 5.4 5.7
12 13绿湾5.5 5.7 5.5 5 5.2 5.8 5.5
13 14旧金山5.6 5.9 6.8 5.1 5.8 5.3
14 15新英格兰5.7 5.4 4.7 5.4 5.9 5.7
15 16 NY喷气式飞机5.7 6.8 6.7 6.0 5.4 5.4
16 17克利夫兰5.7 5.3 5.2 6.0 5.5 5.1
17 18卡罗莱纳州5.8 5.55.3 5.8 5.8 5.4
18 19华盛顿5.8 5.8 6.1 5.7 5.9 5.3
19 20纽约巨人5.8 6.0 4.9 5.7 6.0 5.7
20 21丹佛5.9 6.2 4.8 6.0 5.7 4.9
21 22新奥尔良5.9 4.8 4.7 6.1 5.8 5.4
22 23堪萨斯城6.0 5.4 6.4 5.4 6.4 5.6
23 24费城6.1 7.0 5.6 5.7 6.6 5.2
24 25底特律6.1 5.6 5.4 5.9 6.4 5.5
25 26 LA闸板6.1 6.4 4.8 6.4 5.8 5.3
26 27西雅图6.1 7.2 6.1 6.7 5.8 4.9
27 28亚特兰大6.2 5.1 4.8 6.4 5.9 5.2
28 29辛辛那提6.2 5.7 6.3 6.2 6.2 5.0
29 30迈阿密6.3 6.7 6.3 6.1 6.5 5.4
30 31坦帕湾6.4 6.4 6.8 5.8 7.1 6.0
31 32奥克兰6.6 6.2 6.9 6.5 6.6 5.6]
回溯(最近一次呼叫):
文件“C:\Cabs\projects\nflstatsypp.py公司“,第14行,在
数据框到excel('nflypp.xlsx文件'页,表\u name='yppo',index=False,engine='xlsxwriter')
AttributeError:“list”对象没有“to excel”属性

最后一个?如何清理上面的第二个表,使标题像第一个表一样排列?如果已经回答了,请添加链接。谢谢。注意:当用python打印出来时,第一个表头是正确的,只是为了澄清。再次感谢。不再编辑。希望这一切能有所帮助。你知道吗

我是新来的,只是好玩而已。已经用不同的代码研究了好几个月了。大概有15.5%的人在试着让它工作。你知道吗

谢谢你的帮助。如果答案就在那里,我就找不到也听不懂。:—)最后。再次为自己是个新手感到抱歉。哈哈


Tags: to代码nameimportpandasdfindexhtml
1条回答
网友
1楼 · 发布于 2024-09-30 22:11:11

有几种方法可以做到这一点。我可能会循环压缩代码,在for循环中迭代时保存每个数据帧。但是看起来您的工作表需要不同的名称,这将涉及到以相同的方式创建一个变量来关联每个pd.read_html,而且您似乎是一个初学者,所以我们将尽可能简单地保持这一点,我们将以另一种方式来做,即直接保存数据。你知道吗

首先,当您执行oyyp_df = pd.read_html('https://www.teamrankings.com/nfl/stat/yards-per-play.html',header=0)时,它将其存储为数据帧,但将其打包到一个列表中(请参见here)。你知道吗

另外,回头阅读Python中的列表也是有益的。因此,您的for循环遍历每个列表(oyyp_df, dyyp_df)中的那些项。你知道吗

如果要调用列表中的特定项,可以按其索引/位置进行调用。但要注意的关键是,索引从0开始。因此,列表中的第一项位于位置0,第二项位于位置1,以此类推

a_list = ['first item', 'sencond item, 'third item']

要调用第一个项,您需要键入a_list[0],然后您将看到输出“first item”。你知道吗

现在,列表可以是多种数据类型。它可以是字符串,比如上面提到的,它可以是整数,它可以是字典,或者在这里的例子中,它是数据帧。你知道吗

所以oyyp_df实际上是[<your DATFRAME>, <maybe a 2nd dataframe>, etc.]。你的只有一个项目,在第一个位置。所以你得到了那个错误。列表不能做.to_excel,但数据帧可以。你知道吗

我们能做的是存储第一项数据帧,不过,通过将其设置为另一个名称(或者您实际上可以使用相同的名称…但要小心,因为如果您的列表中有其他项,则会丢失这些项);oyyp_df = oyyp_df[0]

我修改了一些东西,希望能让你下面的代码更清楚。你知道吗

import pandas as pd


html_data1 = pd.read_html('https://www.teamrankings.com/nfl/stat/yards-per-play.html',header=0)
html_data2 = pd.read_html('https://www.teamrankings.com/nfl/stat/opponent-yards-per-play',header=0)


for df in (html_data1, html_data2):
    print(df)


oyyp_df = html_data1[0]
dyyp_df = html_data2[0]


writer = pd.ExcelWriter('nflypp.xlsx')

oyyp_df.to_excel(writer, sheet_name='yppo', index=False)
dyyp_df.to_excel(writer, sheet_name='yppd', index=False)

writer.save()
writer.close()

相关问题 更多 >