Python,如何删除数据框列中的括号,同时';在括号中插入字符串
Python,如何在括号之间有字符串的情况下删除数据帧列中的括号 我有一个名为df_movies的数据帧,看起来像:Python,如何删除数据框列中的括号,同时';在括号中插入字符串,python,Python,Python,如何在括号之间有字符串的情况下删除数据帧列中的括号 我有一个名为df_movies的数据帧,看起来像: movieId title genres 0 1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy 1 2 Jumanji (1995) Adventure|Children|Fantasy 2
movieId title genres
0 1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy
1 2 Jumanji (1995) Adventure|Children|Fantasy
2 3 Grumpier Old Men (1995) Comedy|Romance
3 4 Waiting to Exhale (1995) Comedy|Drama|Romance
4 5 Father of the Bride Part II (1995) Comedy
我只想要标题中的年份号。结果应该是:
1995
1995
1995
1995
1995
问题1:我应该如何获得上述预期结果
问题2:我最初尝试将“()”替换为空白,然后将每行按空格分割,然后获取列表中的最后一项
然而,我尝试了下面的几个代码,它们都不起作用
try 1:
df_movies["title"].str.replace("(","").replace(")","")
result:(only take the first one)
0 Toy Story 1995)
try 2:
Title = df_movies["title"]
Title = Title.replace("(","")
Title = Title.replace(")","")
Title
result:(only take the first one)
0 Toy Story (1995)
However, for try 2, if I only type
Title = df_movies["title"]
Title = Title.replace("(","")
it showed as:
0 Toy Story 1995)
我怎样才能修改它
谢谢使用
.str.extract
:
df['title'].str.extract(r'\((.*)\)')
使用
.str.extract
:
df['title'].str.extract(r'\((.*)\)')
谢谢很有效,谢谢。它起作用了。