Python-Pandas:从列中提取一个数字到新列中

Python-Pandas:从列中提取一个数字到新列中,python,pandas,Python,Pandas,我一直在用python与pandas合作提取信息。我在数据框的一列中有以下标题: 0 In & Out (1997) Simple Plan, A (1998) Retro Puppetmaster (1999) Paralyzing Fear: The Story of Polio in America, A (1998) Old Man and the Sea, The (1958) Body Shots (1999) Coogan's Bluff (1968) Seven S

我一直在用python与pandas合作提取信息。我在数据框的一列中有以下标题:

   0
In & Out (1997)
Simple Plan, A (1998)
Retro Puppetmaster (1999)
Paralyzing Fear: The Story of Polio in America, A (1998)
Old Man and the Sea, The (1958)
Body Shots (1999)
Coogan's Bluff (1968)
Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)
Search for One-eye Jimmy, The (1996)
Funhouse, The (1981)
我想把这些年头的书名写进一个新的专栏。我遇到的问题是,如果我将“(”作为分隔符进行拆分,如第8行所示,它将在那里拆分。那么,我如何在(yyyy)处拆分,以形成一个新的列,该列的年份如下所示

     0                 1
In & Out              1997
Simple Plan, A        1998
Retro Puppetmaster    1999 
Paralyzing Fear:...   1998
Old Man and the S...  1958
Body Shots            1999
Coogan's Bluff        1968 
Seven Samurai (T...   1954
Search for One-ey...  1996
Funhouse, The         1981

您可以尝试字符串切片操作。 string数据类型的rindex()方法返回匹配模式的索引值(在本例中是从右端角开始的“(”)。使用该索引值,我们可以按预期执行字符串切片

例如:

>>> a = "Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)"
>>>
>>> print a[:a.rindex('(')], a[a.rindex('(')+1:-1]

Seven Samurai (The Magnificent Seven) (Shichinin no samurai)  1954    
>>>
>>>
您可以使用扩展:

df['year'] = df.iloc[:,0].str.extract('\((\d{4})\)'',expand=False)

df
Out[381]: 
                                                   0  year
0                                    In & Out (1997)  1997
1                              Simple Plan, A (1998)  1998
2                          Retro Puppetmaster (1999)  1999
3  Paralyzing Fear: The Story of Polio in America...  1998
4                    Old Man and the Sea, The (1958)  1958
5                                  Body Shots (1999)  1999
6                              Coogan's Bluff (1968)  1968
7  Seven Samurai (The Magnificent Seven) (Shichin...  1954
8               Search for One-eye Jimmy, The (1996)  1996
9                               Funhouse, The (1981)  1981

[''.join(c表示x中的c,如果x中的all(c在'0123456789'中,len(x)==4))表示x中的行。split()表示df[1]中的行)]
这并不能完全回答问题。最好作为注释。