ShanZhengYang
ShanZhengYang

Reputation: 17641

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?

Upvotes: 1

Views: 4028

Answers (4)

gzc
gzc

Reputation: 8629

Use + operator, it will concatenate strings automatically.

pd.Series(list_of_strings) + "    " + sr1

Upvotes: 1

jezrael
jezrael

Reputation: 863166

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

Upvotes: 1

user2697520
user2697520

Reputation: 11

How about just turn the list of prefixes into a series of length 250, then add them.

sr0 = pd.Series(list_of_strings)
sr1 = sr0 + sr1

Upvotes: 1

James
James

Reputation: 36691

You can makes a series of your prefixes, then just add the two series together:

import pandas as pd

s1 = pd.Series(['a'*10,'b'*10,'c'*10])

s1
# returns:
# 0    aaaaaaaaaa
# 1    bbbbbbbbbb
# 2    cccccccccc


s2 = pd.Series(['pre1', 'pre2', 'pre3'])

s2+s1
# returns:
# 0    pre1aaaaaaaaaa
# 1    pre2bbbbbbbbbb
# 2    pre3cccccccccc

Upvotes: 1

Related Questions