[英]Python, How to delete brackets in the column of the data frame while there's strings between brackets
Python,當括號之間有字符串時,如何刪除數據框列中的括號。
我有一個名為 df_movies 的數據框,看起來像:
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
Q1:我應該如何獲得上述想要的結果?
Q2:我最初嘗試將“()”替換為空白,然后用空格分隔每一行,然后取出列表中的最后一項。
但是,我嘗試了下面的幾個代碼,它們都沒有工作
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'\((.*)\)')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.