将熊猫中的 2 列映射到第三列

我想'Link'基于其他 2 列(URL和Title)创建pandas 列,以便创建将在表单中保存带有标题的HTML 链接标签的列:


<a href="{}">{}</a>'.format(df['Ad_URL'],df['Title'])


我正在使用:


def Ad_Link(df):

    return ('<a href="{}">{}</a>'.format(df['Ad_URL'],df['Title']))


df['Link'] = df[['Ad_URL','Title']].apply(lambda x:Ad_Link(x), axis=1)

但它没有按预期工作。


它给出: <a href="List of all URLs">List of all Titles</a>对于 df['Link'] 中的所有项目


它假设迭代并给出:


<a href="URL[0]">Title[0]</a> 

<a href="URL[1]">Title[1]</a> 

编辑


实际上我的解决方案奏效了,最初的数据框有问题。


MMTTMM
浏览 196回答 2
2回答

慕无忌1623718

首先你的代码对我很好。但是添加了具有相同输出的多个解决方案:df = pd.DataFrame({'Ad_URL':['u1', 'u2', 'u3'], 'Title':['t1', 't2', 't3']})def Ad_Link(df):&nbsp; &nbsp; return ('<a href="{}">{}</a>'.format(df['Ad_URL'],df['Title']))df['Link'] = df[['Ad_URL','Title']].apply(lambda x:Ad_Link(x), axis=1)#change variable in function to xdef Ad_Link1(x):&nbsp; &nbsp; return ('<a href="{}">{}</a>'.format(x['Ad_URL'],x['Title']))df['Link1'] = df[['Ad_URL','Title']].apply(lambda x:Ad_Link1(x), axis=1)#removed lambda functiondef Ad_Link2(x):&nbsp; &nbsp; return ('<a href="{}">{}</a>'.format(x['Ad_URL'],x['Title']))df['Link2'] = df.apply(Ad_Link2, axis=1)#pass columns names to lambda function, changed functiondef Ad_Link3(url, link):&nbsp; &nbsp; return ('<a href="{}">{}</a>'.format(url, link))df['Link3'] = df.apply(lambda x: Ad_Link3(x['Ad_URL'],x['Title']), axis=1)#only lambda function solutiondf['Link4'] = df.apply(lambda x: '<a href="{}">{}</a>'.format(x['Ad_URL'], x['Title']), axis=1)print (df)&nbsp; Ad_URL Title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Link&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Link1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Link2&nbsp; \0&nbsp; &nbsp; &nbsp;u1&nbsp; &nbsp; t1&nbsp; <a href="u1">t1</a>&nbsp; <a href="u1">t1</a>&nbsp; <a href="u1">t1</a>&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;u2&nbsp; &nbsp; t2&nbsp; <a href="u2">t2</a>&nbsp; <a href="u2">t2</a>&nbsp; <a href="u2">t2</a>&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;u3&nbsp; &nbsp; t3&nbsp; <a href="u3">t3</a>&nbsp; <a href="u3">t3</a>&nbsp; <a href="u3">t3</a>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Link3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Link4&nbsp;&nbsp;0&nbsp; <a href="u1">t1</a>&nbsp; <a href="u1">t1</a>&nbsp;&nbsp;1&nbsp; <a href="u2">t2</a>&nbsp; <a href="u2">t2</a>&nbsp;&nbsp;2&nbsp; <a href="u3">t3</a>&nbsp; <a href="u3">t3</a>&nbsp;&nbsp;

GCT1015

这对我来说很好用。df = pd.DataFrame({'Ad_URL':['u1', 'u2', 'u3'], 'Title':['t1', 't2', 't3']})def Ad_Link(df):&nbsp; &nbsp; return ('<a href="{}">{}</a>'.format(df['Ad_URL'],df['Title']))df['Link'] = df[['Ad_URL','Title']].apply(lambda x:Ad_Link(x), axis=1)print(df)输出:&nbsp; Ad_URL Title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Link0&nbsp; &nbsp; &nbsp;u1&nbsp; &nbsp; t1&nbsp; <a href="u1">t1</a>1&nbsp; &nbsp; &nbsp;u2&nbsp; &nbsp; t2&nbsp; <a href="u2">t2</a>2&nbsp; &nbsp; &nbsp;u3&nbsp; &nbsp; t3&nbsp; <a href="u3">t3</a>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python