使用scipy.io将python pandas dataframe转换为matlab结构 [英] python pandas dataframe to matlab struct using scipy.io
问题描述
我正在尝试使用scipy.io将熊猫数据框保存到matlab .mat文件中.
I am trying to save a pandas dataframe to a matlab .mat file using scipy.io.
我有以下内容:
array1 = np.array([1,2,3])
array2 = np.array(['a','b','c'])
array3 = np.array([1.01,2.02,3.03])
df = DataFrame({1:array1, 2:array2,3:array3}, index=('array1','array2','array3'))
recarray_ = df.to_records()
## Produces:
# rec.array([('array1', 1, 'a', 1.01), ('array2', 2, 'b', 2.02),
# ('array3', 3, 'c', 3.03)],
# dtype=[('index', 'O'), ('1', '<i4'), ('2', 'O'), ('3', '<f8')])
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_records()})
在Matlab中,我希望这会产生一个包含三个数组的结构(一个整数,一个字符,一个浮点数),但实际上它会生成一个包含三个以上结构的结构,每个结构均包含四个变量; 'index',1,'2',3.当尝试选择1,'2'或3时,出现错误信息'变量struct(1,1).#不存在.'
In Matlab, I would expect this to produce a struct containing three arrays (one int, one char, one float) but it actually produces is a struct containing 3 more structs, each containing four variables; 'index', 1, '2', 3. When trying to select 1, '2' or 3 I get the error 'The variable struct(1, 1).# does not exist.'
任何人都可以解释预期的行为以及如何最好地将DataFrame保存到.mat文件吗?
Can anyone explain the expected behaviour and how best to save DataFrames to .mat files?
推荐答案
在此期间,我正在使用以下解决方法.如果您有更好的解决方案,请告诉我:
I am using the following workaround in the meantime. Please let me know if you have a better solution:
a_dict = {col_name : df[col_name].values for col_name in df.columns.values}
## optional if you want to save the index as an array as well:
# a_dict[df.index.name] = df.index.values
scipy.io.savemat('test_struct_to_mat.mat', {'struct':a_dict})
这篇关于使用scipy.io将python pandas dataframe转换为matlab结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!