简体   繁体   English

熊猫发布对DataFrame的迭代

[英]Pandas issue iterating over DataFrame

I'm using pandas to scrape a web page and iterate through a DataFrame object. 我正在使用熊猫来抓取网页并遍历DataFrame对象。 Here's the function I'm calling: 这是我正在调用的函数:

def getTeamRoster(teamURL):
    teamPlayers = []
    table = pd.read_html(requests.get(teamURL).content)[4]
    nameTitle = '\n\t\t\t\tPlayers\n\t\t\t' 
    ratingTitle = 'SinglesRating'
    finalTable = table[[nameTitle, ratingTitle]][:-1]
    print(finalTable)
    for index, row in finalTable:
        print(index, row)

I'm using the syntax advocated here: 我正在使用这里提倡的语法:

http://www.swegler.com/becky/blog/2014/08/06/useful-pandas-snippets/

However, I'm getting this error: 但是,我收到此错误:

File "SquashScraper.py", line 46, in getTeamRoster
    for index, row in finalTable:
ValueError: too many values to unpack (expected 2)

For what it's worth, my finalTable prints as this: 对于它的价值,我的finalTable打印如下:

   \n\t\t\t\tPlayers\n\t\t\t  SinglesRating
0                Browne,Noah           5.56
1             Ellis,Thornton           4.27
2                 Line,James           4.25
3          Desantis,Scott J.           5.08
4           Bahadori,Cameron           4.97
5              Groot,Michael           4.76
6              Ehsani,Darian           4.76
7                 Kardon,Max           4.83
8                 Van,Jeremy           4.66
9     Southmayd,Alexander T.           4.91
10        Cacouris,Stephen A           4.68
11         Groot,Christopher           4.62
12       Mack,Peter D. (sub)           3.94
13      Shrager,Nathaniel O.           0.00
14       Woolverton,Peter C.           4.06

which looks right to me. 在我看来正确。

Any idea why python doesn't like my syntax? 知道为什么python不喜欢我的语法吗?

Thanks for the help, bclayman 感谢您的帮助,bclayman

You probably want to try this: 您可能想尝试一下:

for index, row in finalTable.iterrows():
    print(index, row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM