Search code examples
pythonpandasliststring-concatenation

Apply list over Pandas dataframe


I need to apply a list to a pandas dataframe by column. The operation to be performed is string concatenation. Being more specific:

Inputs I have:

df = pd.DataFrame([['a', 'b', 'c'], ['d', 'e', 'f']], columns=['Col1', 'Col2', 'Col3'])
lt = ['Prod1', 'Prod2', 'Prod3']

which results in:

>>>df
Col1 Col2 Col3
0    a    b    c
1    d    e    f

>>>lt
['Prod1', 'Prod2', 'Prod3']

moreover, the length of lt will always be equal to number of columns of df.

What I would like to have is a dataframe of this sort:

res = pd.DataFrame([['Prod1a', 'Prod2b', 'Prod3c'], ['Prod1d', 'Prod2e', 'Prod3f']],
               columns=['Col1', 'Col2', 'Col3'])

which gives:

>>>res
Col1    Col2    Col3
0  Prod1a  Prod2b  Prod3c
1  Prod1d  Prod2e  Prod3f

Until now, I've been able to solve the problem looping through rows and columns but I won't give up the idea that there's a more elegant way to solve it (maybe something like apply.

Does anyone has suggestions? Thanks!


Solution

  • You can perform broadcasted string concatenation:

    lt + df
    
         Col1    Col2    Col3
    0  Prod1a  Prod2b  Prod3c
    1  Prod1d  Prod2e  Prod3f
    

    You can also use numpy's np.char.add function.

    df[:] = np.char.add(lt, df.values.astype(str))
    df    
         Col1    Col2    Col3
    0  Prod1a  Prod2b  Prod3c
    1  Prod1d  Prod2e  Prod3f
    

    Thirdly, there is the list comprehension option.

    df[:] = [[i + v for i, v in zip(lt, V)] for V in df.values.tolist()]
    df
    
         Col1    Col2    Col3
    0  Prod1a  Prod2b  Prod3c
    1  Prod1d  Prod2e  Prod3f