3 回答

TA貢獻1796條經驗 獲得超4個贊
如果日期格式只是電影標題末尾括號中的年份,請嘗試:
import re
df = pd.DataFrame({'movie':['Toy Story (1995)','Toy Story (no date)','Oddyssey 2000', 'Fort 6600', 'The Matrix (1999)', 'Jumanji', 'Interstellar (2014)']})
df:
movie
0 Toy Story (1995)
1 Toy Story (no date)
2 Oddyssey 2000
3 Fort 6600
4 The Matrix (1999)
5 Jumanji
6 Interstellar (2014)
使用正則表達式:
df[df.movie.apply(lambda x: bool(re.search('\([1-2][0-9]{3}\)$', x)))]
結果:
movie
0 Toy Story (1995)
4 The Matrix (1999)
6 Interstellar (2014)
非年份或不在括號中的數字將不會包含在結果中。我假設年份必須以 1 或 2 開頭。

TA貢獻1797條經驗 獲得超4個贊
這是因為變量i存儲數據的副本,而不是原始引用。
所以,你應該這樣做:
for i in range(len(df['title'])):
if df['title'][i][-1] != ')':
df['title'][i] = ''

TA貢獻1804條經驗 獲得超3個贊
i僅存儲數據,它不是對列表項的引用。
你可以用枚舉來做到這一點:
for index, element in enumerate(df['title']):
if element[-1] != ')':
df['title'][index] = ''
添加回答
舉報