You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Colab中Python处理FBREF爬取数据时出现TypeError:'<'不支持str与int实例间比较的问题排查

Fixing the TypeError When Calculating Per-90 Stats from FBREF MLS Data

Hey there, let's break down why this error is popping up and get your per-90 calculations working smoothly again. Here's what's likely going wrong and how to fix it:

Diagnose the Root Cause

You converted 90s and Gls to float64, but the error points to a string vs integer comparison issue. This usually stems from one of two scenarios:

  1. Other calculation columns are still string types: You only updated 90s and Gls, but columns like Ast, G-PK, and PK might still be stored as object (string) — FBREF often uses - or empty strings for missing values instead of numeric nulls.
  2. Messy index issues: Scraped tables sometimes have multi-level indexes or string values in the index, which triggers type comparison errors when pandas runs element-wise operations.

Step-by-Step Fixes

1. Clean and Convert All Relevant Columns to Numeric Types

First, handle non-numeric placeholders common in FBREF tables, then convert all columns needed for calculations to float:

# Replace non-numeric placeholders (like '-') with 0 or NaN (adjust based on your needs)
dfstandard = dfstandard.replace('-', 0)

# Convert all columns required for per-90 math to float
cols_to_convert = ['90s', 'Gls', 'Ast', 'G-PK', 'PK']
dfstandard[cols_to_convert] = dfstandard[cols_to_convert].astype(float)

Double-check types with print(dfstandard.dtypes) to confirm all target columns are now float64.

2. Reset Your DataFrame Index

If your index has messy values or multi-level headers from the scraped table, resetting it can eliminate comparison errors:

dfstandard = dfstandard.reset_index(drop=True)

3. Test Calculations One Line at a Time

Instead of running all four division lines at once, test them individually to pinpoint which column is causing trouble:

# First test Gls per 90
dfstandard['Gls'] = dfstandard['Gls'] / dfstandard['90s']
print("Gls per 90 calculated successfully")

# Add each subsequent column one by one
dfstandard['Ast'] = dfstandard['Ast'] / dfstandard['90s']
print("Ast per 90 calculated successfully")

# Repeat for G-PK and PK

This helps you confirm if a specific column was missed during type conversion.

4. Verify You Scraped the Correct Table

FBREF pages often have multiple tables (summary, shooting, passing, etc.). Make sure you're selecting the right one from the list returned by pd.read_html():

# Check how many tables are on the page
tables = pd.read_html(html_content)
print(f"Number of scraped tables: {len(tables)}")

# Select the standard stats table (usually the first one)
dfstandard = tables[0]

Final Check

After applying these steps, run your per-90 calculations again — they should execute without the type error. The core issue was almost certainly unprocessed string values in one of your calculation columns or an index that caused incompatible type comparisons.

内容的提问来源于stack exchange,提问作者Gary Davis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:29:07