从电影标题栏中拆分电影年份

2024-04-26 07:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我正在为推荐系统使用电影镜头数据集。我想把电影的年份从标题栏中分出来,放在一个叫做“年份”的新功能中。你知道吗

import re
title = df3.title
df3.Year = re.findall('[(...)]', title)

标题
危险思想(1995)
轨枕(1996)
天堂电影院(Nuovo Cinema Paradiso)(1989)


Tags: 数据import功能re标题电影title系统
2条回答

假设它总是在字符串的末尾:

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)
expr = re.compile('\((....)\)')
df3.Year = re.findall(expr, title)[-1]

相关问题 更多 >