对于数据帧:
Name: Tags:
'One' ['tag1', 'tag3']
'Two' []
'Three' ['tag1']
如何将“tag2”附加到标记列表中
我尝试了以下方法(addtag2是另一个df):
df['Tags'] = df['Tags'].astype(str) + ', ' + addtag2['Tags'].astype(str)
及
df['Tags'] = df['Tags'].add(addtag2['Tags'].astype(str))
但是他们将字符串附加到列表之外,例如['tag1']、tag2或['tag1']tag2
所需的输出将是:
Name: Tags:
'One' ['tag1', 'tag3', 'tag2']
'Two' ['tag2']
'Three' ['tag1', 'tag2']
这是一个
apply
很方便的例子:或者您可以执行for循环:
输出:
或者,您可以使用
append
执行此操作:输出:
相关问题 更多 >
编程相关推荐