Pandas Series of lists to one series

后端 未结 9 2190
野性不改
野性不改 2020-12-28 13:18

I have a Pandas Series of lists of strings:

0                           [slim, waist, man]
1                                [slim, waistline]
2                       


        
9条回答
  •  攒了一身酷
    2020-12-28 13:39

    You can use the list concatenation operator like below -

    lst1 = ['hello','world']
    lst2 = ['bye','world']
    newlst = lst1 + lst2
    print(newlst)
    >> ['hello','world','bye','world']
    

    Or you can use list.extend() function as below -

    lst1 = ['hello','world']
    lst2 = ['bye','world']
    lst1.extend(lst2)
    print(lst1)
    >> ['hello', 'world', 'bye', 'world']
    

    Benefits of using extend function is that it can work on multiple types, where as concatenation operator will only work if both LHS and RHS are lists.

    Other examples of extend function -

    lst1.extend(('Bye','Bye'))
    >> ['hello', 'world', 'Bye', 'Bye']
    

提交回复
热议问题