j45612
j45612

Reputation: 436

Created ndarray from a .mat file, but how to format for a pandas dataframe?

I am loading data from a MATLAB .mat file into python. The .mat file has a number of cells, and within each cell is a struct with several named fields:

samp = 1x2 cell array
{1×1 struct}    {1×1 struct}

samp{1}.buttonID = 3
samp{1}.buttonName = 'name3'

I have loaded the .mat file into python using scipy.io.loadmat(), from which I got this ndarray object:

In [112]: data
Out[113]: 
array([[array([[(array([[3]], dtype=uint8), array(['name3'], dtype='<U10'))]],
      dtype=[('buttonID', 'O'), ('buttonName', 'O')]),
        array([[(array([[1]], dtype=uint8), array(['name1'], dtype='<U6'))]],
      dtype=[('buttonID', 'O'), ('buttonName', 'O')])]], dtype=object)

I have read some articles on how to turn an ndarray into a pandas dataframe, but my confusion is that python has stored the fieldnames ('buttonID' and 'buttonName') with the dtype. How can I pass these names into DataFrame() to correctly store the data?

The goal:

'' 'buttonID' 'buttonName'
0   3         'name3'
1   1         'name1'

Upvotes: 1

Views: 81

Answers (0)

Related Questions