【问题标题】:Iterate two pandas dataframe columns at the same time and return values from each column into separate places同时迭代两个 pandas 数据框列,并将每列的值返回到不同的位置
【发布时间】:2018-09-24 21:53:29
【问题描述】:

我正在寻找一种解决方案,它可以同时迭代两个数据框列,然后从每列中获取值并将它们放在文本中的两个不同位置。

到目前为止我的代码:

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

提前致谢!

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以创建一个新列并将句子的模板和两个参数都放入其中。这将满足您访问原始列中的行值的要求。在下一步中,您可以应用自定义函数,为您创建句子并将它们放入 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'])
    

    使用这种方法,基于如下DataFramedf

        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
    

    【讨论】:

      【解决方案2】:

      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 语句的目标是什么,所以如果您澄清这一点,我可以尝试将其功能插入到上面的函数中......

      【讨论】:

        猜你喜欢
        • 2021-05-09
        • 2015-03-21
        • 2019-03-07
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 2023-01-14
        • 2013-02-04
        • 2019-05-23
        相关资源
        最近更新 更多