使用 scipy.io 将 python pandas 数据帧转换为 matlab 结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30669137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas dataframe to matlab struct using scipy.io
提问by mat4mlw
I am trying to save a pandas dataframe to a matlab .mat file using scipy.io.
我正在尝试使用 scipy.io 将 Pandas 数据帧保存到 matlab .mat 文件中。
I have the following:
我有以下几点:
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()})
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.'
在 Matlab 中,我希望这会生成一个包含三个数组(一个 int、一个 char、一个浮点数)的结构,但它实际上生成的是一个包含另外 3 个结构的结构,每个结构包含四个变量;'index', 1, '2', 3. 尝试选择 1、'2' 或 3 时,出现错误“变量 struct(1, 1).# 不存在”。
Can anyone explain the expected behaviour and how best to save DataFrames to .mat files?
谁能解释预期的行为以及如何最好地将 DataFrames 保存到 .mat 文件?
回答by mat4mlw
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})
回答by Nenad Propadovi?
I think what you need is to create the dataframe like this:
我认为你需要的是像这样创建数据框:
df = DataFrame({'array1':array1, 'array2':array2,'array3':array3})
and save it like this:
并像这样保存:
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_dict("list")})
So the code should be something like:
所以代码应该是这样的:
# ... import appropritely
array1 = np.array([1,2,3])
array2 = np.array(['a','b','c'])
array3 = np.array([1.01,2.02,3.03])
df = DataFrame({'array1':array1, 'array2':array2,'array3':array3})
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_dict("list")})

