How to extract specific content in a pandas dataframe with a regex?

前端 未结 3 1884
Happy的楠姐
Happy的楠姐 2020-12-07 23:35

Consider the following pandas dataframe:

In [114]:

df[\'movie_title\'].head()

​
Out[114]:

0     Toy Story (1995)
1     GoldenEye (1995)
2    Four Rooms (1         


        
3条回答
  •  猫巷女王i
    2020-12-08 00:16

    You should assign text group(s) with () like below to capture specific part of it.

    new_df['just_movie_titles'] = df['movie_title'].str.extract('(.+?) \(')
    new_df['just_movie_titles']
    

    pandas.core.strings.StringMethods.extract

    StringMethods.extract(pat, flags=0, **kwargs)

    Find groups in each string using passed regular expression

提交回复
热议问题