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

拆分Python爬虫抓取的列表以提取NFL球队赔率存入DataFrame

实现方案

核心逻辑是利用正则匹配提取每条记录中的赔率(带小数点的数字),再按赔率位置拆分得到前后两支球队的名称,避免球队名包含多个空格导致的拆分错误。

代码调整部分

你可以在现有代码的末尾追加以下处理逻辑:

import re
import pandas as pd

processed_data = []
for game_str in result:
    # 提取单场比赛的两个赔率值
    odds = re.findall(r'\d+\.\d+', game_str)
    if len(odds) != 2:
        continue # 跳过格式异常的无效场次
    odd1, odd2 = odds
    # 拆分得到两支球队的完整名称
    team1 = game_str.split(odd1)[0].strip()
    team2 = game_str.split(odd1)[1].split(odd2)[0].strip()
    # 适配存入DataFrame的四元素格式,后续可直接生成表格
    processed_data.append([team1, odd1, team2, odd2])
    # 如果你需要示例中提到的单元素带逗号的子列表格式,可替换为下面这行代码
    # processed_data.append([f"{team1}, {odd1}, {team2}, {odd2}"])

# 生成符合要求的DataFrame
df = pd.DataFrame(processed_data, columns=['主队', '主队赔率', '客队', '客队赔率'])

效果说明

原始输入字符串'Buffalo Bills 1.30 Washington Football Team 3.50'会被处理为['Buffalo Bills', '1.30', 'Washington Football Team', '3.50'],可以直接对应DataFrame的四列字段,满足后续展示需求。

内容的提问来源于stack exchange,提问作者Alexander Corry

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 03:24:00