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 ?
The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.
We can use Python's numpy. save() function from transforming an array into a binary file when saving it. This method also can be used to store the dictionary in Python.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With