Convert a dict with array values to pandas dataframe

Question:

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
Asked By: Murilo

||

Answers:

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
Answered By: jezrael

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

df = pd.DataFrame(np.hstack(list(dic.values())), columns=list(dic))

Or stack, slice, then transpose:

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
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.