Joao Figueiredo
Joao Figueiredo

Reputation: 60

Python concat through a list of strings inside a dataframe

I have this df:

name | attach
John | ['0001','0002']
Peter | ['0003']

I need to transform each value in attach list to a link: For example:

name | attach
John | ['http://www.test.com/0001/download', 'http://www.test.com/0002/download']
Peter | ['http://www.test.com/0003/download']

Where each value is the key in link to download.

I tried to use apply func but doesn't worked:

link_part1 = 'http://www.test.com/'
link_part2 = '/download'

df['attach'] = df['attach'].apply(lambda x: x if x is np.NaN else link_part1 + x + link_part2)

the following error is displayed:
TypeError: can only concatenate str (not "list") to str

Upvotes: 0

Views: 65

Answers (2)

captnsupremo
captnsupremo

Reputation: 730

In your example above, the attach column contains lists, so when you attempt to add your two strings link_part1 and link_part2, you get this TypeError, since you can't concatenate these types.

You'll want to do this sort of transformation to every element of the lists in attach. Also, make the code a little cleaner by defining a function, rather than using a lambda in this case. It's a little long for a lambda:

def make_link(attach):
    start = 'http://www.test.com/'
    end = '/download'
    return [f"{start}{x}{end}" for x in attach]

df['attach'] = df['attach'].apply(make_link)

Upvotes: 2

David Wierichs
David Wierichs

Reputation: 545

Take a look at the error message: It tells you that you are trying to concatenate a list to a str, which only can be referring to the '+' operations you use in the lambda function. You almost had it right, though, as you just need to consider the fact that the entries in 'attach' are lists of strings and not strings themselves:

df['attach'] = df['attach'].apply(lambda x: x if x is np.NaN else [link_part1+id+link_part2 for id in x]) 

should work.

Upvotes: 1

Related Questions