Reputation: 3208
Assuming I have the following lists:
comp1 = [
{
"key": "Chrome",
"doc_count": 200798745
},
{
"key": "Mobile Safari",
"doc_count": 79919449
},
{
"key": "WebKit",
"doc_count": 60544508
}
]
comp2 = [
{
"key": "Chrome",
"doc_count": 1231244
},
{
"key": "Mobile Safari",
"doc_count": 882075
}
]
I'd like to transform them into the following Pandas DataFrame:
df = pd.DataFrame({'idx':['Chrome', 'Mobile Safari', 'WebKit'], 'comp1_vals':[200798745, 79919449, 60544508], 'comp2_vals':[1231244, 882075, np.nan]}).set_index('idx')
df
Out[37]:
comp1_vals comp2_vals
idx
Chrome 200798745 1231244.0
Mobile Safari 79919449 882075.0
WebKit 60544508 NaN
What would be the best way to approach it? I thought about transforming each of the lists to a dictionary where the key will become the index of the DataFrame but it looks like it's gonna be messy and contains a lot of if-else statement. Any Pythonic way to do so ?
Upvotes: 0
Views: 41
Reputation: 29742
USe pandas.merge
:
df = pd.merge(pd.DataFrame(comp1), pd.DataFrame(comp2), on='key', how='outer')
print(df)
Output:
key doc_count_x doc_count_y
0 Chrome 200798745 1231244.0
1 Mobile Safari 79919449 882075.0
2 WebKit 60544508 NaN
Upvotes: 4