使用:Google Collab 中的 Python 提前致謝:我已經在其他資料上運行了這段代碼,我已經抓取了 FBREF,所以我不確定為什么現在會發生這種情況。唯一的區別是我刮它的方式。我第一次刮它:
url_link = 'https://fbref.com/en/comps/Big5/gca/players/Big-5-European-Leagues-Stats'
我第二次刮了它:
url = 'https://fbref.com/en/comps/22/stats/Major-League-Soccer-Stats'
html_content = requests.get(url).text.replace('<!--', '').replace('-->', '')
df = pd.read_html(html_content)
然后,我將資料從物件轉換為浮點數,以便在將其拉入資料框后進行計算:
dfstandard['90s'] = dfstandard['90s'].astype(float)
dfstandard['Gls'] = dfstandard['Gls'].astype(float)
我看了看,它表明它們都是花車:
10 90s 743 非空 float64
11 Gls 743 非空 float64
但是當我運行以前作業的代碼時:
dfstandard['Gls'] = dfstandard['Gls'] / dfstandard['90s']
我收到錯誤訊息“TypeError: '<' 在 'str' 和 'int' 的實體之間不支持”
我對抓取還很陌生,我被卡住了,不知道下一步該做什么。
完整的錯誤訊息如下:
<ipython-input-152-e0ab76715b7d> in <module>()
1 #turn data into p 90
----> 2 dfstandard['Gls'] = dfstandard['Gls'] / dfstandard['90s']
3 dfstandard['Ast'] = dfstandard['Ast'] / dfstandard['90s']
4 dfstandard['G-PK'] = dfstandard['G-PK'] / dfstandard['90s']
5 dfstandard['PK'] = dfstandard['PK'] / dfstandard['90s']
8 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in _outer_indexer(self, left, right)
261
262 def _outer_indexer(self, left, right):
--> 263 return libjoin.outer_join_indexer(left, right)```
264
265 _typ = "index"
pandas/_libs/join.pyx in pandas._libs.join.outer_join_indexer()
TypeError: '<' not supported between instances of 'str' and 'int'>
uj5u.com熱心網友回復:
您的資料框中有兩Gls列。我認為您僅將一"Gls"列轉換為浮動,當您這樣做時dfstandard['Gls'] = dfstandard['Gls'] / dfstandard['90s'],正在考慮另一列“Gls”?...
嘗試從列名中洗掉空格
df = df.rename(columns=lambda x: x.strip())
df['90s'] = pd.to_numeric(df['90s'], errors='coerce')
df['Gls'] = pd.to_numeric(df['Gls'], errors='coerce')
因此錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417668.html
標籤:
上一篇:如何從這個Json資料中獲取評論
