首页 > 解决方案 > ValueError: 0 列传递,传递的数据有 3 列

问题描述

我是 pandas 和 BeautifulSoup 的新手,我正在尝试学习关于梦幻足球的教程,并使用来自该网站的数据创建我自己的梦幻篮球(NBA 联盟)版本。它一直给我这个错误:

ValueError: 0 columns passed, passed data had 3 columns

我认为这意味着我的数据中有 3 列,但它们都没有被迭代(或者我可能已经关闭)。我尝试在这里查找类似的问题,但我无法解决这个问题。

这是代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import numpy as np
import math

# NBA season we using (2020)
year = 2020

# url we scraping data from
url = 'https://fantasydata.com/nba/fantasy-basketball-leaders?scope=1&season=2020&seasontype=3&conference=1&scoringsystem=1&date=09-27-2020'.format(year)

# use this to open the url
html = urlopen(url)

soup = bs(html, 'html5lib')
# use findAll() to get column headers
soup.findAll('tr', limit=2)

# use getText() to extract the text we need
headers = [th.getText() for th in soup.findAll('tr', limit=2)[0].findAll('th')]

# exclude first column
headers = headers[1:]

# first row is a no no
rows = soup.findAll('tr')[1:]
player_stats = [[td.getText() for td in rows[i].findAll('td')] for i in range(len(rows))]

df = pd.DataFrame(player_stats, columns=headers)
df.head(529)

标签: pythonpandasbeautifulsoup

解决方案


无需使用硒。只需获取 ajax json 响应并转换为数据框。它是一个嵌套的 json,因此您可能需要根据您的需要做更多的工作,但它就在那里:

import requests
import pandas as pd


# NBA season we using (2020)
year = 2020

# url we scraping data from
url = 'https://fantasydata.com/NBA_FantasyStats/FantasyStats_Read'
payload = {
'sort': 'FantasyPoints-desc',
'pageSize': '500',
'group': '',
'filter': '',
'filters.scope': '1',
'filters.subscope': '',
'filters.season': year,
'filters.seasontype': '3',
'filters.team': '',
'filters.conference': '1',
'filters.position': '',
'filters.searchtext': '',
'filters.scoringsystem': '1',
'filters.exportType': '',
'filters.date': '09-27-2020',
'filters.dfsoperator': '',
'filters.dfsslateid': '',
'filters.dfsslategameid': '',
'filters.dfsrosterslot': '',
'filters.showfavs': '',
'filters.teamkey': '',
'filters.oddsstate': '',
'filters.showall': ''}

data = requests.post(url, params=payload).json()['Data']
df = pd.DataFrame(data)

输出:

print (df.head(10).to_string())
   StatID  TeamID  PlayerID           Name Team Position  Games  Started  Season  Points  Rebounds  OffensiveRebounds  DefensiveRebounds  Assists  BlockedShots  Steals  Turnovers  FieldGoalsMade  FieldGoalsAttempted  FieldGoalsPercentage  FreeThrowsMade  FreeThrowsAttempted  FreeThrowsPercentage  TwoPointersMade  TwoPointersAttempted  TwoPointersPercentage  ThreePointersMade  ThreePointersAttempted  ThreePointersPercentage  Minutes  PersonalFouls  PlusMinus  DoubleDoubles  TripleDoubles  FantasyPoints  FantasyPointsFanDuel  FantasyPointsDraftKings  FantasyPointsYahoo  FantasyPointsFantasyDraft   Day  Date HomeOrAway Opponent Started_YN                    PlayerUrlString                     TeamUrlString GameStatus GameStatusClass  Game  TeamIsHome Result                                        StatSummary  IsScrambled  Rank  StaticRank FantasyPosition PositionRank  IsFavorite
0  798095      20  20001441   Nikola Jokic  DEN        C     19       19    2020   464.0     187.0               37.0              150.0    109.0          15.0    20.0       62.0           178.0                343.0                  51.9            66.0                 79.0                  83.5            136.0                 245.0                   55.5               42.0                    98.0                     42.9      694           68.0      -14.0           10.0            1.0          859.9                 894.9                   937.75               894.9                     937.75  None  None       None     None          N   /nba/nikola-jokic-stats/20001441        /nba/denver-nuggets-roster       None            None  None       False         [{'Items': [{'StatValue': '19', 'StatTitle': '...        False     1           0            None         None       False
1  798490      27  20000571   LeBron James  LAL       SF     16       16    2020   426.0     168.0               17.0              151.0    142.0          15.0    19.0       65.0           155.0                284.0                  54.6            85.0                114.0                  74.6            124.0                 197.0                   62.9               31.0                    87.0                     35.6      561           30.0      139.0           12.0            4.0          843.6                 877.6                   924.00               877.6                     924.00  None  None       None     None          N   /nba/lebron-james-stats/20000571    /nba/los-angeles-lakers-roster       None            None  None       False         [{'Items': [{'StatValue': '16', 'StatTitle': '...        False     2           0            None         None       False
2  798021      20  20001666   Jamal Murray  DEN       PG     19       19    2020   504.0      92.0               20.0               72.0    126.0           6.0    18.0       53.0           186.0                368.0                  50.5            70.0                 78.0                  89.7            124.0                 231.0                   53.7               62.0                   137.0                     45.3      752           42.0        9.0            2.0            0.0          798.4                 822.4                   863.50               822.4                     863.50  None  None       None     None          N   /nba/jamal-murray-stats/20001666        /nba/denver-nuggets-roster       None            None  None       False         [{'Items': [{'StatValue': '19', 'StatTitle': '...        False     3           0            None         None       False
3  798109       9  20001829   Jayson Tatum  BOS       SF     17       17    2020   437.0     170.0               22.0              148.0     85.0          20.0    17.0       48.0           145.0                334.0                  43.4           100.0                123.0                  81.3             98.0                 208.0                   47.1               47.0                   126.0                     37.3      690           41.0       92.0           10.0            0.0          794.5                 831.5                   865.50               831.5                     865.50  None  None       None     None          N   /nba/jayson-tatum-stats/20001829        /nba/boston-celtics-roster       None            None  None       False         [{'Items': [{'StatValue': '17', 'StatTitle': '...        False     4           0            None         None       False
4  798555      27  20000468  Anthony Davis  LAL       PF     16       16    2020   466.0     149.0               38.0              111.0     59.0          21.0    18.0       38.0           160.0                282.0                  56.7           129.0                157.0                  82.2            143.0                 237.0                   60.3               17.0                    45.0                     37.8      577           39.0      158.0            9.0            0.0          773.3                 812.3                   821.75               812.3                     821.75  None  None       None     None          N  /nba/anthony-davis-stats/20000468    /nba/los-angeles-lakers-roster       None            None  None       False         [{'Items': [{'StatValue': '16', 'StatTitle': '...        False     5           0            None         None       False
5  798055      28  20000646  Kawhi Leonard  LAC       SF     13       13    2020   367.0     121.0               24.0               97.0     72.0          11.0    30.0       33.0           131.0                268.0                  48.9            81.0                 94.0                  86.2            107.0                 195.0                   54.9               24.0                    73.0                     32.9      510           29.0       25.0            6.0            0.0          669.2                 710.2                   712.75               710.2                     712.75  None  None       None     None          N  /nba/kawhi-leonard-stats/20000646  /nba/los-angeles-clippers-roster       None            None  None       False         [{'Items': [{'StatValue': '13', 'StatTitle': '...        False     6           0            None         None       False
6  798480       4  20001836    Bam Adebayo  MIA        C     16       16    2020   285.0     175.0               43.0              132.0     74.0          13.0    18.0       39.0           103.0                185.0                  55.7            79.0                 97.0                  81.4            103.0                 184.0                   56.0                0.0                     1.0                      0.0      573           49.0       77.0           10.0            0.0          629.0                 660.0                   672.25               660.0                     672.25  None  None       None     None          N    /nba/bam-adebayo-stats/20001836            /nba/miami-heat-roster       None            None  None       False         [{'Items': [{'StatValue': '16', 'StatTitle': '...        False     7           0            None         None       False
7  798111       9  20001672   Jaylen Brown  BOS       SG     17       17    2020   370.0     127.0               24.0              103.0     39.0           9.0    26.0       41.0           137.0                288.0                  47.6            53.0                 63.0                  84.1             94.0                 168.0                   56.0               43.0                   120.0                     35.8      672           48.0       47.0            2.0            0.0          609.9                 644.9                   661.25               644.9                     661.25  None  None       None     None          N   /nba/jaylen-brown-stats/20001672        /nba/boston-celtics-roster       None            None  None       False         [{'Items': [{'StatValue': '17', 'StatTitle': '...        False     8           0            None         None       False
8  798508      22  20000544   James Harden  HOU       PG     12       12    2020   355.0      67.0               12.0               55.0     92.0          10.0    18.0       45.0           109.0                228.0                  47.8            98.0                116.0                  84.5             70.0                 111.0                   63.1               39.0                   117.0                     33.3      447           43.0       32.0            3.0            0.0          584.4                 612.4                   634.25               612.4                     634.25  None  None       None     None          N   /nba/james-harden-stats/20000544       /nba/houston-rockets-roster       None            None  None       False         [{'Items': [{'StatValue': '12', 'StatTitle': '...        False     9           0            None         None       False
9  798472       4  20000515   Jimmy Butler  MIA       SG     16       16    2020   333.0      88.0               31.0               57.0     73.0           9.0    30.0       43.0            99.0                212.0                  46.7           122.0                143.0                  85.3             86.0                 178.0                   48.3               13.0                    34.0                     38.2      581           29.0       59.0            1.0            0.0          583.1                 622.1                   617.00               622.1                     617.00  None  None       None     None          N   /nba/jimmy-butler-stats/20000515            /nba/miami-heat-roster       None            None  None       False         [{'Items': [{'StatValue': '16', 'StatTitle': '...        False    10           0            None         None       False

推荐阅读