How to zip two lists of tuples by row?

  • A+
Category:Languages

I have two lists like so:

list1 = [{'id':'1','id2':'2'},{'id':'2','id2':'3'}] list2 = [{'fname':'a','lname':'b'},{'fname':'c','lname':'d'}] 

How do I combine the lists into one set of tuples for a pandas dataframe?

like so:

final_list = [{'id':'1','id2':'2','fname':'a','lname':'b'},{'id':'2','id2':'3','fname':'c','lname':'d'}] 

the dataframe should look like this:

id     id2      fname     lname 1       2         a          b 2       3         c          d  

tried this so far:

final_list = list(zip(list1,list2))  df = pd.DataFrame(final_list)  df:  0                          1 [{nested_data}]          [{nested_data}] 

 


You can just use pd.DataFrame.join:

df = pd.DataFrame(list1).join(pd.DataFrame(list2))  print(df)    id id2 fname lname 0  1   2     a     b 1  2   3     c     d 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: