假设我有一个数据帧:
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]
我把其他事情搞砸了,还没把它用上。有什么建议吗
简单的正则表达式如何:
对于数据帧,可以这样做:
最后,如果您真的想将标题和数据分开(在另一个答案中采用Philip的数据帧重建):
完整系列的答案实际上是:
您可以执行以下操作
使用正则表达式查找数字。我使用https://regex101.com/r/Bid0qA/1,这对理解正则表达式的工作原理有很大帮助
相关问题 更多 >
编程相关推荐