简体   繁体   English

Python 通过 dataframe 内的字符串列表连接

[英]Python concat through a list of strings inside a dataframe

I have this df:我有这个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:我尝试使用 apply func 但没有用:

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 TypeError:只能将str(不是“list”)连接到str

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.在上面的示例中, attach列包含列表,因此当您尝试添加两个字符串link_part1link_part2时,您会收到此 TypeError,因为您无法连接这些类型。

You'll want to do this sort of transformation to every element of the lists in attach .您需要对attach中列表的每个元素进行这种转换。 Also, make the code a little cleaner by defining a function, rather than using a lambda in this case.此外,通过定义 function,而不是在本例中使用 lambda,使代码更简洁。 It's a little long for a lambda: 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)

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.看一下错误消息:它告诉您正在尝试将列表连接到 str,这只能指您在 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.应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM