【发布时间】:2014-10-07 04:58:02
【问题描述】:
我在将正则表达式函数应用于 python 数据框中的列时遇到问题。这是我的数据框的头部:
Name Season School G MP FGA 3P 3PA 3P%
74 Joe Dumars 1982-83 McNeese State 29 NaN 487 5 8 0.625
84 Sam Vincent 1982-83 Michigan State 30 1066 401 5 11 0.455
176 Gerald Wilkins 1982-83 Chattanooga 30 820 350 0 2 0.000
177 Gerald Wilkins 1983-84 Chattanooga 23 737 297 3 10 0.300
243 Delaney Rudd 1982-83 Wake Forest 32 1004 324 13 29 0.448
我认为我已经很好地掌握了将函数应用于 Dataframes 的能力,所以我的 Regex 技能可能有所欠缺。
这是我整理的:
import re
def split_it(year):
return re.findall('(\d\d\d\d)', year)
df['Season2'] = df['Season'].apply(split_it(x))
TypeError: expected string or buffer
输出将是一个名为 Season2 的列,其中包含连字符之前的年份。我确信没有正则表达式有一种更简单的方法,但更重要的是,我试图找出我做错了什么
提前感谢您的帮助。
【问题讨论】: