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

如何将DataFrame中胜-负-平格式的record列(含NC)拆分为4个独立列

拆分record列的pandas实现方案

直接使用正则匹配提取所有字段即可,代码示例如下:

import pandas as pd

# 假设df为你持有的原始DataFrame
# 正则规则:匹配胜-负-平,以及可选的NC标识
pat = r'^(\d+)-(\d+)-(\d+)(?:\s*\((\d+)\s*NC\))?$'
df[['win', 'lose', 'draw', 'nc']] = df['record'].str.extract(pat)

# 转换为可空整数类型
df[['win', 'lose', 'draw', 'nc']] = df[['win', 'lose', 'draw', 'nc']].astype('Int64')
# 若需要nc列空值为Python原生None,补充执行以下代码
df['nc'] = df['nc'].where(df['nc'].notna(), None)

效果验证

用测试数据运行后输出示例:

recordwinlosedrawnc
10-3-01030None
35-11-2 (1 NC)351121
5-0-0500None
22-5-3 (2 NC)22532

内容的提问来源于stack exchange,提问作者le Minh Nguyen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 12:15:01