Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving pandas data frame to .mat file in python3

I have a pandas data frame 'df', it looks like below but original data has many rows.

enter image description here

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;

enter image description here

How can I fix this, thanks.

like image 570
kutlus Avatar asked Jun 20 '26 06:06

kutlus


1 Answers

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()}
like image 195
Kyle Avatar answered Jun 21 '26 22:06

Kyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!