Python Pandas 将列中的子字符串替换为另一列中的子字符串

Python Pandas replace sub-string in column with sub-string in another column

我有一个 Python Pandas 数据框,如下所示:

Id      Title            URL                                       PosterPath
Id-1    Bruce Almighty   https://www.youtube.com/embed/5VGyTOGxyVA  https://i.ytimg.com/vi/XXXRRR/hqdefault.jpg
Id-2    Superhero Movie  https://www.youtube.com/embed/3BnXz-7-y-o  https://i.ytimg.com/vi/XXXRRR/hqdefault.jpg
Id-3    Taken            https://www.youtube.com/embed/vjbfiOERDYs  https://i.ytimg.com/vi/XXXRRR/hqdefault.jpg

我想将 PosterPath 列中的子字符串 "XXXRRR" 替换为 "URL" 列中字符串 "embed/" 之后的子字符串 输出数据框如下所示:

Id      Title            URL                                       PosterPath
Id-1    Bruce Almighty   https://www.youtube.com/embed/5VGyTOGxyVA  https://i.ytimg.com/vi/5VGyTOGxyVA/hqdefault.jpg
Id-2    Superhero Movie  https://www.youtube.com/embed/3BnXz-7-y-o  https://i.ytimg.com/vi/3BnXz-7-y-o/hqdefault.jpg
Id-3    Taken            https://www.youtube.com/embed/vjbfiOERDYs  https://i.ytimg.com/vi/vjbfiOERDYs/hqdefault.jpg

使用str.extract with Series.replace:

a = df['URL'].str.extract('embed/(.*)$', expand=False)
print (a)
0    5VGyTOGxyVA
1    3BnXz-7-y-o
2    vjbfiOERDYs
Name: URL, dtype: object

df['PosterPath'] = df['PosterPath'].replace('XXXRRR', a, regex=True)
print (df)
     Id            Title                                        URL  \
0  Id-1   Bruce Almighty  https://www.youtube.com/embed/5VGyTOGxyVA   
1  Id-2  Superhero Movie  https://www.youtube.com/embed/3BnXz-7-y-o   
2  Id-3            Taken  https://www.youtube.com/embed/vjbfiOERDYs   

                                         PosterPath  
0  https://i.ytimg.com/vi/5VGyTOGxyVA/hqdefault.jpg  
1  https://i.ytimg.com/vi/3BnXz-7-y-o/hqdefault.jpg  
2  https://i.ytimg.com/vi/vjbfiOERDYs/hqdefault.jpg