I get a error of that the length of the of the passed value 0?
This is my code:
import bs4 as bs
import urllib
import urllib.request
import pandas as pd
draft2018 ="https://en.wikipedia.org/wiki/2018_NBA_draft"
draftpage =urllib.request.urlopen(draft2018)
soup=bs.BeautifulSoup(draftpage,"html.parser")
columns = ['Round', 'Pick', 'Player', 'Position',
'Nationality', 'Team', 'School/club team']
df = pd.DataFrame(columns=columns)
table = soup.find("table",{"class":"wikitable sortable plainrowheaders"}).tbody
trs = table.find_all("tr")
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('\n','') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)
Can somebody explain the reasoning behind this?
Use read_html
for return list of DataFrames and select 4. DataFrame by indexing [3]
, then rename
columns by dictionary:
draft2018 = "https://en.wikipedia.org/wiki/2018_NBA_draft"
d = {'Rnd.':'Round','Pos.':'Position','Nationality[n 1]':'Nationality'}
df = pd.read_html(draft2018)[3].rename(columns=d)
print(df.head())
Round Pick Player Position Nationality \
0 1 1 Deandre Ayton C Bahamas
1 1 2 Marvin Bagley III PF United States
2 1 3 Luka Dončić PG/SF Slovenia
3 1 4 Jaren Jackson Jr. PF United States
4 1 5 Trae Young PG United States
Team School / club team
0 Phoenix Suns Arizona (Fr.)
1 Sacramento Kings Duke (Fr.)
2 Atlanta Hawks (traded to Dallas)[a] Real Madrid (Spain)
3 Memphis Grizzlies Michigan State (Fr.)
4 Dallas Mavericks (traded to Atlanta)[a] Oklahoma (Fr.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With