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.
CodePudding user response:
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
CodePudding user response:
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