Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip list elements in different dataframe columns

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

like image 366
user11638654 Avatar asked Jun 22 '19 10:06

user11638654


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)]
like image 85
jezrael Avatar answered Oct 16 '22 09:10

jezrael