Issue
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.
Solution
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
Answered By - ArchAngelPwn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.