matlab数据文件到pandas DataFrame [英] matlab data file to pandas DataFrame
问题描述
是否存在将 matlab .mat
(matlab格式的数据)文件转换为Panda DataFrame
的标准方法?
Is there a standard way to convert matlab .mat
(matlab formated data) files to Panda DataFrame
?
我知道使用scipy.io
可以解决问题,但是我想知道是否有直接的方法.
I am aware that a workaround is possible by using scipy.io
but I am wondering whether there is a straightforward way to do it.
推荐答案
我发现了2种方法:scipy或mat4py.
I found 2 way: scipy or mat4py.
- mat4py
从MAT文件加载数据
Load data from MAT-file
loadmat函数将存储在MAT文件中的所有变量加载到 简单的Python数据结构,仅使用Python的字典和列表 对象.数字和单元格数组转换为按行顺序嵌套 列表.压缩数组以消除仅包含一个元素的数组. 结果数据结构由以下几种简单类型组成: 与JSON格式兼容.
The function loadmat loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict and list objects. Numeric and cell arrays are converted to row-ordered nested lists. Arrays are squeezed to eliminate arrays with only one element. The resulting data structure is composed of simple types that are compatible with the JSON format.
示例:将MAT文件加载到Python数据结构中:
Example: Load a MAT-file into a Python data structure:
data = loadmat('datafile.mat')
发件人:
https://pypi.python.org/pypi/mat4py/0.1.0
- Scipy:
示例:
import numpy as np
from scipy.io import loadmat # this is the SciPy module that loads mat-files
import matplotlib.pyplot as plt
from datetime import datetime, date, time
import pandas as pd
mat = loadmat('measured_data.mat') # load mat-file
mdata = mat['measuredData'] # variable in mat file
mdtype = mdata.dtype # dtypes of structures are "unsized objects"
# * SciPy reads in structures as structured NumPy arrays of dtype object
# * The size of the array is the size of the structure array, not the number
# elements in any particular field. The shape defaults to 2-dimensional.
# * For convenience make a dictionary of the data using the names from dtypes
# * Since the structure has only one element, but is 2-D, index it at [0, 0]
ndata = {n: mdata[n][0, 0] for n in mdtype.names}
# Reconstruct the columns of the data table from just the time series
# Use the number of intervals to test if a field is a column or metadata
columns = [n for n, v in ndata.iteritems() if v.size == ndata['numIntervals']]
# now make a data frame, setting the time stamps as the index
df = pd.DataFrame(np.concatenate([ndata[c] for c in columns], axis=1),
index=[datetime(*ts) for ts in ndata['timestamps']],
columns=columns)
发件人:
http://poquitopicante.blogspot. fr/2014/05/loading-matlab-mat-file-into-pandas.html
- 最后,您可以使用PyHogs,但仍然可以使用scipy:
读取复杂的
.mat
文件.
此笔记本显示了读取Matlab .mat文件的示例, 将数据转换为带有循环的简单字典,简单绘图 数据.
This notebook shows an example of reading a Matlab .mat file, converting the data into a usable dictionary with loops, a simple plot of the data.
http://pyhogs.github.io/reading-mat-files.html
这篇关于matlab数据文件到pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!