Pandas read_html 返回的列在 Python 中具有 NaN 值
Pandas read_html returned column with NaN values in Python
我正在尝试使用 Pandas read.html 函数解析位于 here 的 table。我能够解析 table。但是,列容量随 NaN
一起返回。我不确定,reason.I 想要解析整个 table 并将其用于进一步研究的可能是什么。所以任何帮助表示赞赏。下面是我到目前为止的代码..
wiki_url='Above url'
df1=pd.read_html(wiki_url,index_col=0)
Pandas 只能获取上标(无论出于何种原因)而不是实际值,如果您打印所有 df1 并检查 Capacity 列,您将看到一些值是 [ 1]、[2] 等(如果它们有脚注),否则为 NaN。
您可能想研究获取数据的替代方法,或者使用 BeautifulSoup 自己抓取数据,因为 Pandas 正在查找并因此返回了错误的数据。
尝试这样的事情(包括 flavor
作为 bs4
):
df = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_NCAA_Division_I_FBS_football_stadiums',header=[0],flavor='bs4')
df = df[0]
print(df.head())
Image Stadium City State \
0 NaN Aggie Memorial Stadium Las Cruces NM
1 NaN Alamodome San Antonio TX
2 NaN Alaska Airlines Field at Husky Stadium Seattle WA
3 NaN Albertsons Stadium Boise ID
4 NaN Allen E. Paulson Stadium Statesboro GA
Team Conference Capacity \
0 New Mexico State Independent 30,343[1]
1 UTSA C-USA 65000
2 Washington Pac-12 70,500[2]
3 Boise State Mountain West 36,387[3]
4 Georgia Southern Sun Belt 25000
.............................
.............................
要替换方括号下的任何内容,请使用:
df.Capacity = df.Capacity.str.replace(r"\[.*\]","")
print(df.Capacity.head())
0 30,343
1 65000
2 70,500
3 36,387
4 25000
希望对您有所帮助。
@anky_91 发布的答案是正确的。我想尝试另一种不使用 Regex 的方法。以下是我不使用 Regex 的解决方案。
df4=pd.read_html('https://en.wikipedia.org/wiki/List_of_NCAA_Division_I_FBS_football_stadiums',header=[0],flavor='bs4')
df4 = df4[0]
解决方案是删除第 1 行和第 4 行中@anky_91 提供的 "r"
print(df4.Capacity.head())
0 30,343
1 65000
2 70,500
3 36,387
4 25000
Name: Capacity, dtype: object
我正在尝试使用 Pandas read.html 函数解析位于 here 的 table。我能够解析 table。但是,列容量随 NaN
一起返回。我不确定,reason.I 想要解析整个 table 并将其用于进一步研究的可能是什么。所以任何帮助表示赞赏。下面是我到目前为止的代码..
wiki_url='Above url'
df1=pd.read_html(wiki_url,index_col=0)
Pandas 只能获取上标(无论出于何种原因)而不是实际值,如果您打印所有 df1 并检查 Capacity 列,您将看到一些值是 [ 1]、[2] 等(如果它们有脚注),否则为 NaN。
您可能想研究获取数据的替代方法,或者使用 BeautifulSoup 自己抓取数据,因为 Pandas 正在查找并因此返回了错误的数据。
尝试这样的事情(包括 flavor
作为 bs4
):
df = pd.read_html(r'https://en.wikipedia.org/wiki/List_of_NCAA_Division_I_FBS_football_stadiums',header=[0],flavor='bs4')
df = df[0]
print(df.head())
Image Stadium City State \
0 NaN Aggie Memorial Stadium Las Cruces NM
1 NaN Alamodome San Antonio TX
2 NaN Alaska Airlines Field at Husky Stadium Seattle WA
3 NaN Albertsons Stadium Boise ID
4 NaN Allen E. Paulson Stadium Statesboro GA
Team Conference Capacity \
0 New Mexico State Independent 30,343[1]
1 UTSA C-USA 65000
2 Washington Pac-12 70,500[2]
3 Boise State Mountain West 36,387[3]
4 Georgia Southern Sun Belt 25000
.............................
.............................
要替换方括号下的任何内容,请使用:
df.Capacity = df.Capacity.str.replace(r"\[.*\]","")
print(df.Capacity.head())
0 30,343
1 65000
2 70,500
3 36,387
4 25000
希望对您有所帮助。
@anky_91 发布的答案是正确的。我想尝试另一种不使用 Regex 的方法。以下是我不使用 Regex 的解决方案。
df4=pd.read_html('https://en.wikipedia.org/wiki/List_of_NCAA_Division_I_FBS_football_stadiums',header=[0],flavor='bs4')
df4 = df4[0]
解决方案是删除第 1 行和第 4 行中@anky_91 提供的 "r"
print(df4.Capacity.head())
0 30,343
1 65000
2 70,500
3 36,387
4 25000
Name: Capacity, dtype: object