2 回答
TA贡献1744条经验 获得超4个赞
首先你的代码对我很好。但是添加了具有相同输出的多个解决方案:
df = pd.DataFrame({'Ad_URL':['u1', 'u2', 'u3'], 'Title':['t1', 't2', 't3']})
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)
#change variable in function to x
def Ad_Link1(x):
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 function
def Ad_Link2(x):
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 function
def Ad_Link3(url, link):
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 solution
df['Link4'] = df.apply(lambda x: '<a href="{}">{}</a>'.format(x['Ad_URL'], x['Title']), axis=1)
print (df)
Ad_URL Title Link Link1 Link2 \
0 u1 t1 <a href="u1">t1</a> <a href="u1">t1</a> <a href="u1">t1</a>
1 u2 t2 <a href="u2">t2</a> <a href="u2">t2</a> <a href="u2">t2</a>
2 u3 t3 <a href="u3">t3</a> <a href="u3">t3</a> <a href="u3">t3</a>
Link3 Link4
0 <a href="u1">t1</a> <a href="u1">t1</a>
1 <a href="u2">t2</a> <a href="u2">t2</a>
2 <a href="u3">t3</a> <a href="u3">t3</a>
TA贡献1827条经验 获得超4个赞
这对我来说很好用。
df = pd.DataFrame({'Ad_URL':['u1', 'u2', 'u3'], 'Title':['t1', 't2', 't3']})
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)
print(df)
输出:
Ad_URL Title Link
0 u1 t1 <a href="u1">t1</a>
1 u2 t2 <a href="u2">t2</a>
2 u3 t3 <a href="u3">t3</a>
添加回答
举报