我试图在pandas数据框中创建一个新列,该列包含字符串前缀和来自另一列的值。包含值的列具有多个逗号分隔值的示例。例如:
MIMNumber
102610
114080,601079
我希望dataframe看起来像这样:
MIMNumber OMIM_Link
102610 https://www.omim.org/entry/102610
114080,601079 https://www.omim.org/entry/114080,https://www.omim.org/entry/601079
我试过这个:
df['OMIM_Link'] = df['MIMNumber'].map('https://www.omim.org/entry/{}'.format)
但这并没有将字符串前缀添加到所有有多个逗号分隔值的示例中:
MIMNumber OMIM_Link
102610 https://www.omim.org/entry/102610
114080,601079 https://www.omim.org/entry/114080,601079
我也试过这个:
url = 'https://www.omim.org/entry/'
df['OMIM_Link'] = df['MIMNumber'].apply(url.join)
但是字符串前缀连接在每个值之间:
MIMNumber OMIM_Link
102610 1https://www.omim.org/entry/0https://www.omim.org/entry/2https://www.omim.org/entry/6https://www.omim.org/entry/1https://www.omim.org/entry/0
114080,601079 1https://www.omim.org/entry/1https://www.omim.org/entry/4https://www.omim.org/entry/0https://www.omim.org/entry/8https://www.omim.org/entry/0https://www.omim.org/entry/,https://www.omim.org/entry/6https://www.omim.org/entry/0https://www.omim.org/entry/1https://www.omim.org/entry/0https://www.omim.org/entry/7https://www.omim.org/entry/9
有什么建议吗?
3条答案
按热度按时间oymdgrw71#
你可以试试regex replace
cwtwac6a2#
将逗号替换为
,https://www.omim.org/entry/
,并在开头添加https://www.omim.org/entry/
。5m1hhzi43#
如果你有多种域/路径,就把它放在这里:
它可以做你想要的: