Home > Blockchain >  Create pandas DataFrame from numpy array of dictionaries
Create pandas DataFrame from numpy array of dictionaries

Time:01-17

I have very long numpy array that has multiple dictionnaries as elements:

np.array([{'col1': 'somevalue', 'col2': 2}, {'col1': 'someotherval', 'col2': 4}, {'col1': 'zzzzz', 'col2': 47}], dtype=object)

Is there any way to create a pandas DataFrame where each dictionary would be a row?

Result should be:

col1 col2
'somevalue' 2
'someotherval' 4
'zzzzz' 47

Also conerting the numpy array to a list would not work as I need to keep memory usage low, so I can't go with pd.DataFrame(list(my_array))

CodePudding user response:

You can use iter() to avoid making an intermediate list:

pd.DataFrame(iter(arr))

This outputs:

           col1  col2
0     somevalue     2
1  someotherval     4
2         zzzzz    47
  • Related