Home > OS >  Convert a dict with array values to pandas dataframe
Convert a dict with array values to pandas dataframe

Time:09-20

I have a dictionary with string as keys and arrays as values, like this:

dic = {'x': array([[10],
       [27],
       [12],
       [132]]), 'y': array([[-39],
       [23],
       [42],
       [98]]), 'z': array([[-100],
       [-123],
       [92],
       [88.2]])}

How could i convert it to a pandas dataframe in the following format:

iter x   y   z 
0   10 -39 -100
1   27  23 -123
2   12  42   92
3   132 98  88.2

Tried the following:

df = pd.DataFrame.from_dict(dic)

Getting the following error:

ValueError: Per-column arrays must each be 1-dimensional

CodePudding user response:

Use dict comprehension with numpy.ravel:

df = pd.DataFrame({k: np.ravel(v) for k, v in dic.items()})                  
print (df)
     x   y      z
0   10 -39 -100.0
1   27  23 -123.0
2   12  42   92.0
3  132  98   88.2

Or use map:

df = pd.DataFrame(map(np.ravel, dic.values()), index=dic.keys()).T                
print (df)
       x     y      z
0   10.0 -39.0 -100.0
1   27.0  23.0 -123.0
2   12.0  42.0   92.0
3  132.0  98.0   88.2

CodePudding user response:

You can stack the numpy arrays and pass them to the DataFrame constructor:

df = pd.DataFrame(np.stack(dic.values())[...,0], index=list(dic)).T

output:

       x     y      z
0   10.0 -39.0 -100.0
1   27.0  23.0 -123.0
2   12.0  42.0   92.0
3  132.0  98.0   88.2
  • Related