Skip to content
Advertisement

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement