13

Let the given dictionaries are

d = [{'a':1,'b':4},{'b':2}]

So basically I want a matrix like this

  | 'a' | 'b'  |
 _______________
  |  1  |  4   |
  |  na |  2   |

How can I efficiently achieve this ?

Tamim Addari
  • 7,591
  • 9
  • 40
  • 59

1 Answers1

15

The Pandas DataFrame constructor will immediately give you the result you are looking for:

import pandas as pd
pd.DataFrame(d).values

The .values part on the end converts the result to a NumPy array, which is what you asked for. Some people would just work directly with the DataFrame instead.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 12
    Using DataFrame might be convenient, but if speed is a concern it seems like overkill. I've read a (hopefully credible) article that specifically warns about the cost of creating a DataFrame: https://www.linkedin.com/pulse/careful-when-using-sklearn-pandas-production-environment-longbin-chen/. So, I'm curious what the most efficient method would be if we wanted to avoid the overhead of creating a Pandas DataFrame. – Regorsmitz May 06 '19 at 23:32