Python 如何从包含文本的数据框中的列中提取年份(或日期时间)
假设我有一个数据帧:Python 如何从包含文本的数据框中的列中提取年份(或日期时间),python,regex,pandas,datetime,parsing,Python,Regex,Pandas,Datetime,Parsing,假设我有一个数据帧: 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
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()
及
我把其他事情搞砸了,还没把它用上。有什么建议吗?简单的正则表达式怎么样:
text = 'Harry Potter (1997)'
re.findall('\((\d{4})\)', text)
# ['1997'] Note that this is a list of "all" the occurrences.
对于数据帧,可以这样做:
text = 'Harry Potter (1997)'
df = pd.DataFrame({'Book': text}, index=[1])
pattern = '\((\d{4})\)'
df['year'] = df.Book.str.extract(pattern, expand=False) #False returns a series
df
# Book year
# 1 Harry Potter (1997) 1997
最后,如果您真的想在另一个答案中分离标题和从Philip获取数据帧重建的数据:
df = pd.DataFrame(columns=['Book'], data=[['Harry Potter (1997)'],['Of Mice and Men (1937)'],['Babe Ruth Story, The (1948) Drama 948) Babe Ruth Story']])
sep = df['Book'].str.extract('(.*)\((\d{4})\)', expand=False)
sep # A new df, separated into title and year
# 0 1
# 0 Harry Potter 1997
# 1 Of Mice and Men 1937
# 2 Babe Ruth Story, The 1948
简单的正则表达式如何:
text = 'Harry Potter (1997)'
re.findall('\((\d{4})\)', text)
# ['1997'] Note that this is a list of "all" the occurrences.
对于数据帧,可以这样做:
text = 'Harry Potter (1997)'
df = pd.DataFrame({'Book': text}, index=[1])
pattern = '\((\d{4})\)'
df['year'] = df.Book.str.extract(pattern, expand=False) #False returns a series
df
# Book year
# 1 Harry Potter (1997) 1997
最后,如果您真的想在另一个答案中分离标题和从Philip获取数据帧重建的数据:
df = pd.DataFrame(columns=['Book'], data=[['Harry Potter (1997)'],['Of Mice and Men (1937)'],['Babe Ruth Story, The (1948) Drama 948) Babe Ruth Story']])
sep = df['Book'].str.extract('(.*)\((\d{4})\)', expand=False)
sep # A new df, separated into title and year
# 0 1
# 0 Harry Potter 1997
# 1 Of Mice and Men 1937
# 2 Babe Ruth Story, The 1948
您可以执行以下操作
import pandas as pd
df = pd.DataFrame(columns=['id','Book'], data=[[1,'Harry Potter (1997)'],[2,'Of Mice and Men (1937)'],[3,'Babe Ruth Story, The (1948) Drama 948) Babe Ruth Story']])
df['Year'] = df['Book'].str.extract(r'(?!\()\b(\d+){1}')
行:进口熊猫
行:创建数据帧以便于理解
行:创建一个新的“年”列,该列是从列Book上的字符串提取创建的。
使用正则表达式查找数字。我使用,这对理解正则表达式的工作原理有很大帮助。您可以执行以下操作
import pandas as pd
df = pd.DataFrame(columns=['id','Book'], data=[[1,'Harry Potter (1997)'],[2,'Of Mice and Men (1937)'],[3,'Babe Ruth Story, The (1948) Drama 948) Babe Ruth Story']])
df['Year'] = df['Book'].str.extract(r'(?!\()\b(\d+){1}')
行:进口熊猫
行:创建数据帧以便于理解
行:创建一个新的“年”列,该列是从列Book上的字符串提取创建的。
使用正则表达式查找数字。我使用,这对理解正则表达式的工作原理有很大帮助。整个系列的答案实际上是:
books['title'].str.findall('\((\d{4})\)').str.get(0)
完整系列的答案实际上是:
books['title'].str.findall('\((\d{4})\)').str.get(0)
美好的这正是我想要的。如果我这样做,我将不得不使用for循环遍历pandas系列中的所有值,这是非常缓慢的。这帮助我得到了正确的答案,就是:books['title'].str.findall'\d{4}.str.get0Whoops我是说df['books']。我会将您的答案标记为正确。@MattElgazar请参阅我的上一次更新以提取标题=谢谢您的有趣问题;我学到了一些东西来解决这个问题!这正是我想要的。如果我这样做,我将不得不使用for循环遍历pandas系列中的所有值,这是非常缓慢的。这帮助我得到了正确的答案,就是:books['title'].str.findall'\d{4}.str.get0Whoops我是说df['books']。我会将您的答案标记为正确。@MattElgazar请参阅我的上一次更新以提取标题=谢谢您的有趣问题;我学到了一些东西,弄明白了这一点,这实际上适用于我所介绍的案例,但并不适用于所有案例。看看这个例子。一些书名2002ah我正要给你写信。它确实起作用了。发布更多的数据,以便我们有更多的工作。如果你在这一过程中增加了更多的案例,那么你就很难真正回答这个问题。我已经更新了我的答案这实际上适用于我所介绍的案例,但并不适用于所有案例。看看这个例子。一些书名2002ah我正要给你写信。它确实起作用了。发布更多的数据,以便我们有更多的工作。如果你在这一过程中增加了更多的案例,那么你就很难真正回答这个问题。我已经更新了我的答案