将Mat文件转换为pandas数据框 [英] Convert mat file to pandas dataframe
本文介绍了将Mat文件转换为pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将
import pandas as pd
import scipy.io
mat = scipy.io.loadmat('cardio.mat')
cardio_df = pd.DataFrame(mat)
我收到此错误:
例外:数据必须是一维的
推荐答案
看来 mat
是一个字典,其中包含形状为的
, X
(1831, 21) y
(形状为(1831,1)
)和一些元数据。假设 X
是数据,而 y
是同一标签,则可以使用<$ c $水平堆叠它们c> np.hstack 并将它们加载到熊猫中:
It seems mat
is a dict containing X
of shape (1831, 21)
, y
with shape (1831, 1)
, and some metadata. Assuming X
is the data and y
are the labels for the same, you can stack them horizontally with np.hstack
and load them into 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)
这篇关于将Mat文件转换为pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文