如何用 python pandas 替换部分字符串?
How to replace part of string with python pandas?
我在替换 Pandas 中的部分字符串时遇到问题。
我有一个这样的网站的链接列表(例如):
https://whosebug.com/questions
https://whosebug.com/some_page
https://whosebug.com/about
我想用 link/
替换 https://whosebug.com/
。
应该是这样的:
link/questions
link/some_page
link/about
我试过类似的方法,但它替换了整个字符串:
df.loc[df['links'].str.contains('https://whosebug.com/'), 'links'] = 'link/'
links
是列的名称
我该怎么做?
提前致谢。
只要 URL 一致,这应该可以满足您的需求。
data = {
'Column1' : ['https://whosebug.com/questions', 'https://whosebug.com/another', 'https://whosebug.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://whosebug.com/'), 'links'] = \
'link/' + df[0].str.extract('.*/(.*)')
0 links
0 https://whosebug.com/questions link/questions
1 https://whosebug.com/some_page link/some_page
2 https://whosebug.com/about link/about
你需要 .str.replace
作业
df.loc[df['links'].str.contains('https://whosebug.com/'), 'links'] = df['links'].str.replace('https://whosebug.com/', 'link/')
print(df)
links
0 link/questions
1 link/some_page
2 link/about
3 https://Whosebug/questions
我在替换 Pandas 中的部分字符串时遇到问题。
我有一个这样的网站的链接列表(例如):
https://whosebug.com/questions
https://whosebug.com/some_page
https://whosebug.com/about
我想用 link/
替换 https://whosebug.com/
。
应该是这样的:
link/questions
link/some_page
link/about
我试过类似的方法,但它替换了整个字符串:
df.loc[df['links'].str.contains('https://whosebug.com/'), 'links'] = 'link/'
links
是列的名称
我该怎么做?
提前致谢。
只要 URL 一致,这应该可以满足您的需求。
data = {
'Column1' : ['https://whosebug.com/questions', 'https://whosebug.com/another', 'https://whosebug.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://whosebug.com/'), 'links'] = \
'link/' + df[0].str.extract('.*/(.*)')
0 links
0 https://whosebug.com/questions link/questions
1 https://whosebug.com/some_page link/some_page
2 https://whosebug.com/about link/about
你需要 .str.replace
作业
df.loc[df['links'].str.contains('https://whosebug.com/'), 'links'] = df['links'].str.replace('https://whosebug.com/', 'link/')
print(df)
links
0 link/questions
1 link/some_page
2 link/about
3 https://Whosebug/questions