简体   繁体   中英

Python 3.x: Create dataframe from two dictionaries

I'm working on Python 3.x. What is to be achieved is: merge dictionaries based on keys and form a dataframe. This would clear:

What I have:

import numpy as np
import pandas as pd

d1 = {(1, "Autumn"): np.array([2.5, 4.5, 7.5, 9.5]), (1, "Spring"): np.array([10.5, 11.7, 12.3, 15.0])}
d2 = {(1, "Autumn"): np.array([10.2, 13.3, 15.7, 18.8]), (1, "Spring"): np.array([15.6, 20, 23, 27])}

What I want to achieve:

d3 = {(1, "Autumn"): pd.DataFrame([[2.5, 10.2], [4.5, 13.3], [7.5, 15.7], [9.5, 18.8]], 
  columns = ["d1", "d2"]), (1, "Spring"): pd.DataFrame([[10.5, 15.6], [11.7, 20], 
            [12.3, 23], [15.0, 27]], columns = ["d1", "d2"])}

PS: I'm actually working on RandomForestRegressor example. The above dictionaries are my X and y values after the train and test data splits. What I'm trying to achieve is to get X, y side-by-side in a dataframe for plots with above query. The size of dictionary is same as are the key and number of values for each key in both dictionaries.

Since all keys are present in both dictionaries (according to your comment), you could iterate through the keys of one dictionary and make a dataframe from each dictionary entry for each key:

d3 = dict()
for k in d1.keys():
    d3[k] = pd.DataFrame(np.array([d1[k],d2[k]]).T, columns=["d1","d2"])

Output:

{(1, 'Autumn'):   
    d1    d2
 0  2.5  10.2
 1  4.5  13.3
 2  7.5  15.7
 3  9.5  18.8, 
(1, 'Spring'):    
    d1    d2
 0  10.5  15.6
 1  11.7  20.0
 2  12.3  23.0
 3  15.0  27.0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM