【发布时间】:2019-04-18 21:27:16
【问题描述】:
假设我有一个 pandas 数据框:
Id Book
1 Harry Potter (1997)
2 Of Mice and Men (1937)
3 Babe Ruth Story, The (1948) Drama 948) Babe Ruth Story
如何从列中提取年份?
输出应该是:
Id Book Title Year
1 Harry Potter 1997
2 Of Mice and Men 1937
3 Babe Ruth Story, The 1948
到目前为止我已经尝试过:
movies['year'] = movies['title'].str.extract('([0-9(0-9)]+)', expand=False).str.strip()
和
books['year'] = books['title'].str[-5:-1]
我搞砸了一些其他的事情,还没有让它工作。有什么建议吗?
【问题讨论】:
标签: python regex pandas datetime parsing