【问题标题】:Connecting words in columns连接列中的单词
【发布时间】:2019-06-10 14:41:22
【问题描述】:

背景

我有以下代码

import pandas as pd
#create df
df = pd.DataFrame({'Before' : ['there are many different', 
                               'i like a lot of sports ', 
                               'the middle east has many '], 
                   'After' : ['in the bright blue box', 
                               'because they go really fast ', 
                               'to ride and have fun '],

                  'P_ID': [1,2,3], 
                  'Word' : ['crayons', 'cars', 'camels'],
                  'N_ID' : ['A1', 'A2', 'A3']

                 })

#rearrange
df = df[['P_ID', 'N_ID', 'Before', 'Word','After']]

创建以下df

  P_ID  N_ID    Before                 Words       After
0   1   A1   there are many different   crayons     in the bright blue box
1   2   A2  i like a lot of sports      cars      because they go really fast
2   3   A3  the middle east has many    camels      to ride and have fun

目标

1) 将BeforeAfter 列中的单词与Word 列中的单词联系起来

2) 创建new_column

期望的输出

具有以下输出的new_column

new_column
there are many different crayons in the bright blue box
i like a lot of sports cars because they go really fast
the middle east has many camels to ride and have fun

问题

我如何实现我的目标?

【问题讨论】:

    标签: python string pandas dataframe nlp


    【解决方案1】:

    您可以只添加这些列:

    df['new_column'] = df['Before'] + ' ' + df['Word'] + ' ' + df['After']

    这里是完整的代码:

    import pandas as pd
    #create df
    df = pd.DataFrame({'Before' : ['there are many different', 
                                   'i like a lot of sports ', 
                                   'the middle east has many '], 
                       'After' : ['in the bright blue box', 
                                   'because they go really fast ', 
                                   'to ride and have fun '],
    
                      'P_ID': [1,2,3], 
                      'Word' : ['crayons', 'cars', 'camels'],
                      'N_ID' : ['A1', 'A2', 'A3']
    
                     })
    
    #rearrange
    df = df[['P_ID', 'N_ID', 'Word', 'Before', 'After']]
    df['new_column'] = df['Before'] + ' ' + df['Word'] + ' ' + df['After']
    df['new_column']
    
    0    there are many different crayons in the bright...
    1    i like a lot of sports  cars because they go r...
    2    the middle east has many  camels to ride and h...
    Name: new_column, dtype: object
    

    【讨论】:

      【解决方案2】:

      您可以按照上面的建议添加列或更通用的解决方案来解决可能发生的许多类似问题

      df['new_column']=df.apply(lambda x: x.Before+x.Word+x.After, axis=1)
      

      【讨论】:

        【解决方案3】:

        可以使用 .str 访问器的方法 cat()

        df['New_column'] = df['Before'].str.cat(df[['Word','After']],sep=" ")
        
        • cat() 甚至允许您添加分隔符
        • 加入多列只需将系列列表或包含除第一列之外的所有列的数据框作为参数传递给在第一列(之前)上调用的 str.cat():

        代码:

        import pandas as pd
        #create df
        df = pd.DataFrame({'Before' : ['there are many different',
                                       'i like a lot of sports ',
                                       'the middle east has many '],
                           'After' : ['in the bright blue box',
                                       'because they go really fast ',
                                       'to ride and have fun '],
        
                          'P_ID': [1,2,3],
                          'Word' : ['crayons', 'cars', 'camels'],
                          'N_ID' : ['A1', 'A2', 'A3']
        
                         })
        
        #rearrange
        df = df[['P_ID', 'N_ID', 'Before', 'Word','After']]
        print (df)
        df['New_column'] = df['Before'].str.cat(df[['Word','After']],sep=" ")
        print (df)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-24
          • 2020-09-04
          • 2019-05-12
          • 1970-01-01
          • 2020-04-21
          • 2019-08-07
          • 1970-01-01
          相关资源
          最近更新 更多