同时迭代两个pandas数据帧列,并将每个列的值返回到不同的位置

时间:2018-04-14 19:57:16

标签: python pandas 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 ['关键字']获取,并且我将这些值返回到df ['类别元标题']中的两个地方。我不想添加两次,而是将df ['类别标题']中的值添加为次要值。

结果如下:

+---------+----------------+---------------------------------------------------------------+
| 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     |
+---------+----------------+---------------------------------------------------------------+

提前致谢!

2 个答案:

答案 0 :(得分:0)

IIUC,此功能将使用str.format语法而不是f'{string}'格式执行您所需的操作:

def fetchingMetaTitle(row):
    title1 = '{} - We have a great selection of {} | Example.com'.format(
                     row['Keyword'].title(), row['Category Title'])
    title2 = '{} - Choose among several {} here | Example.com'.format(
                     row['Keyword'].title(), row['Category Title'])
    title3 = '{} - Buy cheap {} easy and fast | Example.com'.format(
                     row['Keyword'].title(), row['Category Title'])
    return random.choice([title1,title2,title3])

df['Category Meta Title '] = df.apply(fetchingMetaTitle, axis=1)

>>> df
   Keyword Category Title                               Category Meta Title 
0    jeans     blue jeans  Jeans - Choose among several blue jeans here |...
1  jackets    red jackets  Jackets - We have a great selection of red jac...
2    shoes    black shoes  Shoes - Buy cheap black shoes easy and fast | ...

或者,使用f'{string}'方法:

def fetchingMetaTitle(row):
    keyword = row['Keyword'].title()
    cat = row['Category Title']
    title1 = f'{keyword} - We have a great selection of {cat} | Example.com'
    title2 = f'{keyword} - Choose among several {cat} here | Example.com'
    title3 = f'{keyword} - Buy cheap {cat} easy and fast | Example.com'
    return random.choice([title1,title2,title3])

df['Category Meta Title '] = df.apply(fetchingMetaTitle, axis=1)

会做同样的事情。

注意:我不确定你的if声明的目标是什么,所以如果你澄清一下,我可以尝试将其功能插入到上面的函数中......

答案 1 :(得分:0)

您可以创建一个新列并将句子的模板和两个参数放在其中。这将满足您访问两个原始列中的行值的要求。在下一步中,您可以应用自定义函数为您创建句子并将其放在res列中。

import pandas as pd

df = pd.DataFrame({'A':['aa','bb','cc'], 'B':['a','b','c'], 'C':['1.{}, {}', '2.{}, {}', '3.{}, {}']})

df['combined'] = df[['A','B','C']].values.tolist()
df['res'] = df['combined'].apply(lambda x: x[2].format(x[0], x[1]))

print(df['res'])

使用此方法,基于以下DataFrame df

    A  B         C
0  aa  a  1.{}, {}
1  bb  b  2.{}, {}
2  cc  c  3.{}, {}

输出结果为:

0    1.aa, a
1    2.bb, b
2    3.cc, c