将python数据框转换为Matlab文件 [英] Converting python Dataframe to Matlab file

查看:126
本文介绍了将python数据框转换为Matlab文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将python数据框转换为Matlab(.mat)文件.

I am trying to convert a python Dataframe to a Matlab (.mat) file.

我最初有一个使用panda.read_csv导入的txt(EEG信号):

I initially have a txt (EEG signal) that I import using panda.read_csv:

MyDataFrame = pd.read_csv("data.txt",sep=';',decimal='.'),data.txt是带有标签的2D数组.这样会创建一个类似于的数据框.

MyDataFrame = pd.read_csv("data.txt",sep=';',decimal='.'), data.txt being a 2D array with labels. This creates a dataframe which looks like this.

为了将其转换为.mat,我尝试了

In order to convert it to .mat, I tried this solution where the idea is to convert the dataframe into a dictionary of lists but after trying every aspect of this solution it's still unsuccessful.

scipy.io.savemat('EEG_data.mat', {'struct':MyDataFrame.to_dict("list")})

它确实创建了.mat文件,但没有正确保存我的数据框.我获得的文件看起来像,因此所有值基本上都消失了,并且当您查看它们时,看到的其余标签为空.

It did create a .mat file but it did not save my dataframe properly. The file I obtain after looks like this, so all the values are basically gone, and the remaining labels you see are empty when you look into them.

我还尝试使用 mat4py ,其旨在将python结构导出到Matlab文件,但也无法正常工作.我不明白为什么,因为根据mat4py文档,将数据框完全转换为列表字典确实是应该做的事情.

I also tried using mat4py which is designed to export python structures into Matlab files, but it did not work either. I don't understand why, because converting my dataframe to a dictionary of lists is exactly what should be done according to the mat4py documentation.

推荐答案

我相信以前的解决方案不适合您的原因是您的DataFrame列名称不是有效的MATLAB struct字段名称,因为它们包含空格和/或以数字字符开头.

I believe that the reason the previous solutions haven't worked for you is that your DataFrame column names are not valid MATLAB struct field names, because they contain spaces and/or start with digit characters.

当我这样做时:

import pandas as pd
import scipy.io
MyDataFrame = pd.read_csv('eeg.txt',sep=';',decimal='.')
truncDataFrame = MyDataFrame[0:1000] # reduce data size for test purposes
scipy.io.savemat('EEGdata1.mat', {'struct1':truncDataFrame.to_dict("list")})

MATLAB中的结果是具有4个字段reltimedatetimeiSensorquality的结构.这些元素每个都有1000个元素,因此这些列中的数据已经转换,但是其余数据丢失了.

the result in MATLAB is a struct with the 4 fields reltime, datetime, iSensor and quality. Each of these has 1000 elements, so the data from these columns has been converted, but the rest of your data is missing.

但是,如果我先重命名DataFrame列:

However if I first rename the DataFrame columns:

truncDataFrame.rename(columns=lambda x:'col_' + x.replace(' ', '_'), inplace=True)  
scipy.io.savemat('EEGdata2.mat', {'struct2':truncDataFrame.to_dict("list")})

MATLAB中的结果是具有36个字段的结构.这与您的mat4py解决方案格式不同,但据我所知,它确实包含了来自源DataFrame的所有数据.

the result in MATLAB is a struct with 36 fields. This is not the same format as your mat4py solution but it does contain (as far as I can see) all the data from the source DataFrame.

(请注意,在您的问题中,您正在创建一个.mat文件,其中包含一个名为struct的变量,并将其加载到MATLAB中时会掩盖内置的struct数据类型-这也可能导致后续MATLAB出现问题代码.)

(Note that in your question, you are creating a .mat file that contains a variable called struct and when this is loaded into MATLAB it masks the builtin struct datatype - that might also cause issues with subsequent MATLAB code.)

这篇关于将python数据框转换为Matlab文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆