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

如何移除DataFrame中Opponent列内的数字?

移除DataFrame中Opponent列的数字

你已经完成ESPN大学橄榄球赛程的抓取与DataFrame格式化,现在只需清理Opponent列里的排名数字,以下是几种实现方式:

方法1:正则替换数字(最直接)

用正则匹配所有数字并替换为空,再清理多余空格:

# 移除数字并清理前后空格
schedule['Opponent'] = schedule['Opponent'].str.replace(r'\d+', '', regex=True).str.strip()
  • r'\d+' 匹配一个或多个连续数字
  • str.strip() 去掉替换后可能残留的前后空格(比如12 Wisconsin替换后会变成 Wisconsin,strip后得到Wisconsin)

方法2:提取球队名称(精准匹配)

如果只想保留字母和空格组成的球队名,用str.extract提取目标内容:

schedule['Opponent'] = schedule['Opponent'].str.extract(r'([A-Za-z\s]+)', expand=False).str.strip()
  • r'([A-Za-z\s]+)' 匹配字母和空格的组合,直接提取球队名称部分

方法3:整合到原有处理流程

可以把这一步加到你原来的Opponent列清理链中,一次性完成所有替换:

schedule['Opponent'] = schedule['Opponent'].str.replace("vs", '') \
    .str.replace("*", '') \
    .str.replace("@", '') \
    .str.replace(r'\d+', '', regex=True) \
    .str.strip()

处理后结果示例:

Date      Opponent
0   20210409    Wisconsin
1   20211109    Ball State
2   20211809    Auburn
3   20212509    Villanova
4   20210210    Indiana
5   20210910    Iowa
6   20212310    Illinois
7   20213010    Ohio State
8   20210611    Maryland
9   20211311    Michigan
10  20212011    Rutgers
11  20212711    Michigan State
12  20210101    Arkansas

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 21:45:37