我正在寻找一种解决方案,可以同时迭代两个dataframe列,然后从每个列中获取值,并将它们放在文本中两个不同的位置。你知道吗
到目前为止我的代码是:
def fetchingMetaTitle(x):
keywords = df['Keyword']
title1 = f'{x.title()} - We have a great selection of {x} | Example.com'
title2 = f'{x.title()} - Choose among several {x} here | Example.com'
title3 = f'{x.title()} - Buy cheap {x} easy and fast | Example.com'
for i in keywords:
if i.lower() in x.lower():
return random.choice([title1,title2,title3])
else:
return np.nan
df['Category Meta Title'] = df['Keyword'].apply(fetchingMetaTitle)
结果如下:
+---------+----------------+-----------------------------------------------------------+
| Keyword | Category Title | Category Meta Title |
+---------+----------------+-----------------------------------------------------------+
| jeans | blue jeans | Jeans - We have a great selection of jeans | Example.com |
| jackets | red jackets | Jackets - Choose among several jackets here | Example.com |
| shoes | black shoes | Shoes - Buy cheap shoes easy and fast | Example.com |
+---------+----------------+-----------------------------------------------------------+
目前,我只从df['Keyword']获取值,并在两个位置将值返回到df['Category Meta Title']。我不想添加两次,而是将df['Category Title']中的值添加为第二个值。你知道吗
结果如下:
+---------+----------------+---------------------------------------------------------------+
| Keyword | Category Title | Category Meta Title |
+---------+----------------+---------------------------------------------------------------+
| jeans | blue jeans | Jeans - We have a great selection of blue jeans | Example.com |
| jackets | red jackets | Jackets - Choose among several red jackets here | Example.com |
| shoes | black shoes | Shoes - Buy cheap black shoes easy and fast | Example.com |
+---------+----------------+---------------------------------------------------------------+
提前谢谢!你知道吗
您可以创建一个新列,并将一个句子的模板和这两个参数放在其中。这将满足您对访问两个原始列中的行值的要求。在下一步中,您可以应用一个自定义函数,该函数为您创建句子并将它们放在
res
列中。你知道吗使用此方法,基于以下数据帧
df
:输出为:
IIUC,此函数将使用^{} 语法而不是
f'{string}'
格式执行您需要的操作:或者,使用
f'{string}'
方法:会做同样的事。你知道吗
注意:我不太清楚您的
if
语句的目标是什么,因此如果您澄清这一点,我可以尝试将其功能插入到上面的函数中。。。你知道吗相关问题 更多 >
编程相关推荐