回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试使用scipy.io将pandas数据帧保存到一个matlab.mat文件中。</p>
<p>我有以下资料:</p>
<pre><code>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()})
</code></pre>
<p>在Matlab中,我希望它生成一个包含三个数组(一个int,一个char,一个float)的结构,但实际上它生成的是一个包含三个以上结构的结构,每个都包含四个变量:“index”,1,“2”,3。尝试选择1、2或3时,出现错误“变量结构(1、1)。”不存在</p>
<p>有人能解释一下预期的行为以及如何最好地将数据帧保存到.mat文件吗?</p>