zip list elements in different dataframe columns

Viewed 2704
a=[['1','2'],['3','4']]
b=[['5','6'],['7','8']]
df14=pd.DataFrame({'key':a,'hi':b})

i want to add a 3rd column with tuples, where each list element from 'key' is matched with its list index in 'hi' -> e.g. that it looks like this.

       key    hi      tup
0   [1, 2]  [5, 6]  [(1,5),(2,6)]
1   [3, 4]  [7, 8]  [(3,7),(4,8)]

i know that i have to use the zip function, but i cant get thy syntax right. i think it should be something like

for index,row in df14.iterrows():
   df14['tup']=df14.key.apply(lambda x: zip(x,df14.hi))

but this is somehow wrong

1 Answers

Here iterrows is not necessary, second row is modify for apply lambda function per row by axis=1 and changed d14.hi for x.hi for return values per row of hi column:

df14['tup'] = df14.apply(lambda x: list(zip(x.key,x.hi)), axis=1)
print (df14)
      key      hi               tup
0  [1, 2]  [5, 6]  [(1, 5), (2, 6)]
1  [3, 4]  [7, 8]  [(3, 7), (4, 8)]
Related