
我正在为推荐系统使用电影镜头数据集。我想把电影的年份从标题栏中分割出来,并把它放在一个名为“年份”的新功能中。
import re
title = df3.title
df3.Year = re.findall('[(...)]', title)标题
危险心理(1995年)
露宿者(1996年)
Paradiso电影院(Nuovo电影院Paradiso) (1989年)
发布于 2017-05-26 04:47:35
假设它总是在字符串的末尾:
rgx = re.compile(r"(?:\((\d{4})\))?\s*$")
match = rgx.search(txt)
# group 1 will be None if not matched else eg '1989'
year = match.group(1)发布于 2017-05-26 04:49:09
expr = re.compile('\((....)\)')
df3.Year = re.findall(expr, title)[-1]https://stackoverflow.com/questions/44193739
复制相似问题