Search code examples
pythonpandasstring-concatenationseries

How to append a list element to each row in a pandas Series?


I have the following pandas Series, whereby each row is a long string with no spaces. It is shaped (250,) (i.e. there are 250 rows)

import pandas as pd
sr1 = pd.Series(...)

                                                     0  
0    abdcadbcadcbadacbadbdddddacbadcbadadbcadbcadad...  
1    cacacdacadbdcadcabdcbadcbadbdabcabdbbbbbacdbac...  
2    bbbbbcadcacddabcadbcdabcbaddcbadcbadbcadbcaaba...  
3    acdbcdacdbadbadcbdbaaaacbdacadbacaddcabdacbdab...  
....

I have a list of 250 strings which I would like to append to the beginning of each of the rows.

list_of_strings = ["prefix1", "prefix2", "prefix3", ...., "prefix250"]

How does one append each element in list_of_strings to the corresponding row in sr1? The resulting Series should look like this:

                                                    0  
0    prefix1    abdcadbcadcbadacbadbdddddacbadcbadadbcadbcadad...  
1    prefix2    cacacdacadbdcadcabdcbadcbadbdabcabdbbbbbacdbac...  
2    prefix3    bbbbbcadcacddabcadbcdabcbaddcbadcbadbcadbcaaba...  
3    prefix4    acdbcdacdbadbadcbdbaaaacbdacadbacaddcabdacbdab...  
....

My first thought was to try something like:

sr1.insert(0, "prefixes", value = list_of_strings)

But this throws the error AttributeError: 'Series' object has no attribute 'insert'. One could convert sr1 to a pandas DataFrame with sr1 = sr1.to_frame(), and the previous .insert() will result in a DataFrame with two columns.

In python, we can concatenate strings with a specified delimiter as follows:

first = "firstword"
second = "secondword"
combined = "    ".join([first, second])
## outputs 'firstword    secondword'

I'm not sure how this is down with a pandas Series. Perhaps .apply(' '.join) somehow?


Solution

  • You need first create Series from list and then add double add or + - one for whitespace and another for s:

    s = pd.Series(['a','b','c'])
    list_of_strings = ["prefix1", "prefix2", "prefix3"]
    
    print (pd.Series(list_of_strings, index=s.index).add(' ').add(s))
    #same as
    #print (pd.Series(list_of_strings, index=s.index)+ ' ' + s)
    0    prefix1 a
    1    prefix2 b
    2    prefix3 c
    dtype: object
    

    Another solution with cat:

    print (pd.Series(list_of_strings, index=s.index).str.cat(s, sep=' '))
    0    prefix1 a
    1    prefix2 b
    2    prefix3 c
    dtype: object
    

    Solution with apply, but need DataFrame - by constructor or by concat:

    print (pd.DataFrame({'prefix':list_of_strings, 'vals':s}).apply(' '.join, axis=1))
    0    prefix1 a
    1    prefix2 b
    2    prefix3 c
    dtype: object
    

    print (pd.concat([pd.Series(list_of_strings, index=s.index), s], axis=1)
             .apply(' '.join, axis=1))
    0    prefix1 a
    1    prefix2 b
    2    prefix3 c
    dtype: object