Reputation: 361
I have a pandas data frame 'df', it looks like below but original data has many rows.
I would like to save this as .mat file with a name 'meta.mat'. I tried;
import scipy.io as sio
sio.savemat(os.path.join(destination_folder_path,'meta.mat'), df)
This creates the meta.mat file but it only writes the field names, when I open it in matlab it looks like this;
How can I fix this, thanks.
Upvotes: 7
Views: 12479
Reputation: 1
This is another solution. The resulting mat file will in the form of a structure in matlab
# data dictionary
OutData = {}
# convert DF to dictionary before loading to your dictionary
OutData['Obj'] = df.to_dict('list')
sio.savemat('path\\testmat.mat',OutData)
Upvotes: 0
Reputation: 2894
I don't think you can pass a pd.DataFrame
directly when scipy.io.savemat
is expecting a dict of numpy
arrays. Try replacing df
with the following in your call to savemat:
{name: col.values for name, col in df.items()}
Upvotes: 10