14

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 ?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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: linkedin.com/pulse/…. So, I'm curious what the most efficient method would be if we wanted to avoid the overhead of creating a Pandas DataFrame.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.