RustyShackleford
RustyShackleford

Reputation: 3667

How to zip two lists of tuples by row?

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}]

Upvotes: 4

Views: 1085

Answers (3)

Josh Friedlander
Josh Friedlander

Reputation: 11657

A "pure" Python answer (ie no Pandas):

[{**x[0], **x[1]} for x in zip(list1, list2)]

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

Edited by Scott Boston

pd.DataFrame([{**x[0], **x[1]} for x in zip(list1, list2)])

Output:

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

Upvotes: 5

IMCoins
IMCoins

Reputation: 3306

You should do pd.concat.

As per the documentation, it seems that @jpp answer is better in terms of performance. I'd be more inclined to believe a benchmark, but honestly, I trust the pandas documentation.

import pandas as pd

df = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)

result_df = pd.concat([df, df2], axis=1)

#result_df
#  id id2 fname lname
#0  1   2     a     b
#1  2   3     c     d

Upvotes: 5

jpp
jpp

Reputation: 164663

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

Upvotes: 4

Related Questions