1

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
1
  • not sure where your input is coming from, but if you remove the 'array()' elements you'll get almost what you are looking for. If you also eliminate the sub lists, you'll get what you want. dic = {'x': [10, 27, 12, 132], 'y': [-39, 23, 42, 98], 'z': [-100, -123, 92, 88.2]} Commented Sep 20, 2022 at 12:58

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.