Python pandas dataframe for matlab structure using scipy.io

I am trying to save the pandas framework to a matlab.mat file using scipy.io.

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 create a structure containing three arrays (one int, one char, one float), but in fact it creates a structure containing 3 more structures, each of which contains four variables; 'index', 1, '2', 3. When I try to select 1, '2' or 3, I get the error message 'Variable struct (1, 1). # does not exist.'

Can someone explain the expected behavior and what is the best way to save DataFrames in .mat files?

+4
source share
2 answers

. , , :

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})
+2

, :

df = DataFrame({'array1':array1, 'array2':array2,'array3':array3})

:

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

, :

# ... 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")})
+1

All Articles