将mat文件转换为pandas数据帧

时间:2017-07-10 08:22:04

标签: python pandas dataframe mat

我想将this file转换为pandas dataframe。

import pandas as pd
import scipy.io
mat = scipy.io.loadmat('cardio.mat')
cardio_df = pd.DataFrame(mat)

我收到此错误: Exception: Data must be 1-dimensional

1 个答案:

答案 0 :(得分:6)

似乎mat是包含X形状(1831, 21)y形状(1831, 1)和某些元数据的字典。假设X是数据而y是相同的标签,您可以将它们与np.hstack水平堆叠并将它们加载到pandas中:

In [1755]: mat = scipy.io.loadmat('cardio.mat')

In [1758]: cardio_df = pd.DataFrame(np.hstack((mat['X'], mat['y'])))

In [1759]: cardio_df.head()
Out[1759]: 
         0         1         2         3         4         5         6   \
0  0.004912  0.693191 -0.203640  0.595322  0.353190 -0.061401 -0.278295   
1  0.110729 -0.079903 -0.203640  1.268942  0.396246 -0.061401 -0.278295   
2  0.216546 -0.272445 -0.203640  1.050988  0.148753 -0.061401 -0.278295   
3  0.004912  0.727346 -0.203640  1.212171 -0.683598 -0.061401 -0.278295   
4 -0.100905  0.363595  1.321366  1.027120  0.141359 -0.061401 -0.278295   

In [1760]: cardio_df.shape
Out[1760]: (1831, 22)