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

时间:2017-06-16 10:40:17

标签: python-2.7 pandas str-replace

我有一个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

我想替换子字符串" XXXRRR"来自PosterPath列,带有字符串" embed /"来自专栏" URL" 输出数据框如下所示:

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

1 个答案:

答案 0 :(得分:4)

str.extract使用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