简体   繁体   中英

How to replace part of string with python pandas?

I'm having a problem replacing a part of the string in Pandas.

I have a list of links of a website like this (for example):

https://stackoverflow.com/questions
https://stackoverflow.com/some_page
https://stackoverflow.com/about

and I would like to replace https://stackoverflow.com/ with link/ .

And it should be like this:

link/questions
link/some_page
link/about

I tried something like this but it replaces the whole string:

df.loc[df['links'].str.contains('https://stackoverflow.com/'), 'links'] = 'link/'

links is the name of column

How do I do this?

Thanks in advance.

This should get you what you need as long as the URLs are consistent.

data = {
    'Column1' : ['https://stackoverflow.com/questions', 'https://stackoverflow.com/another', 'https://stackoverflow.com/last']
}

df = pd.DataFrame(data)
df['Column1'] = df['Column1'].apply(lambda x : 'Link/' + x.split('/')[-1])
df
df.loc[df['links'].str.contains('https://stackoverflow.com/'), 'links'] = \
    'link/' + df[0].str.extract('.*/(.*)') 

                                     0           links
0  https://stackoverflow.com/questions  link/questions
1  https://stackoverflow.com/some_page  link/some_page
2      https://stackoverflow.com/about      link/about

You need .str.replace in assignment

df.loc[df['links'].str.contains('https://stackoverflow.com/'), 'links'] = df['links'].str.replace('https://stackoverflow.com/', 'link/')
print(df)

                             links
0                   link/questions
1                   link/some_page
2                       link/about
3  https://stackoverflow/questions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM