matlab 数据文件到 Pandas DataFrame [英] matlab data file to pandas DataFrame
问题描述
是否有一种标准的方法可以将 matlab .mat
(matlab 格式的数据)文件转换为 Panda DataFrame
?
我知道可以通过使用 scipy.io
来解决问题,但我想知道是否有一种直接的方法可以做到这一点.
我找到了 2 种方式:scipy 或 mat4py.
- mat4py
<块引用>
从 MAT 文件加载数据
函数 loadmat 将存储在 MAT 文件中的所有变量加载到一个简单的 Python 数据结构,仅使用 Python 的 dict 和 list对象.数值和元胞数组转换为行序嵌套列表.数组被压缩以消除只有一个元素的数组.生成的数据结构由简单的类型组成,这些类型是兼容JSON格式.
示例:将 MAT 文件加载到 Python 数据结构中:
data = loadmat('datafile.mat')
来自:
https://pypi.python.org/pypi/mat4py/0.1.0
- Scipy:
示例:
将 numpy 导入为 npfrom scipy.io import loadmat # 这是加载 mat 文件的 SciPy 模块导入 matplotlib.pyplot 作为 plt从日期时间导入日期时间,日期,时间将熊猫导入为 pdmat = loadmat('measured_data.mat') # 加载 mat 文件mdata = mat['measuredData'] # mat 文件中的变量mdtype = mdata.dtype # 结构的 dtypes 是未确定大小的对象"# * SciPy 将结构读取为 dtype 对象的结构化 NumPy 数组# * 数组的大小是结构数组的大小,不是数字# 任何特定字段中的元素.形状默认为二维.# * 为方便起见,使用 dtypes 中的名称制作数据字典# * 由于结构只有一个元素,而且是二维的,因此将其索引在 [0, 0]ndata = {n: mdata[n][0, 0] for n in mdtype.names}# 仅从时间序列重建数据表的列# 使用区间数来测试一个字段是列还是元数据columns = [n for n, v in ndata.iteritems() if v.size == ndata['numIntervals']]# 现在制作一个数据框,将时间戳设置为索引df = pd.DataFrame(np.concatenate([ndata[c] for c in columns],axis=1),index=[datetime(*ts) for ts in ndata['timestamps']],列=列)
来自:
http://poquitopicante.blogspot.fr/2014/05/loading-matlab-mat-file-into-pandas.html
- 最后,您可以使用 PyHogs 但仍然使用 scipy:
<块引用>
读取复杂的 .mat
文件.
本笔记本显示了读取 Matlab .mat 文件的示例,将数据转换为带有循环的可用字典,一个简单的图数据.
http://pyhogs.github.io/reading-mat-files.html
Is there a standard way to convert matlab .mat
(matlab formated data) files to Panda DataFrame
?
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.
I found 2 way: scipy or mat4py.
- mat4py
Load data from MAT-file
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.
Example: Load a MAT-file into a Python data structure:
data = loadmat('datafile.mat')
From:
https://pypi.python.org/pypi/mat4py/0.1.0
- Scipy:
Example:
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)
From:
http://poquitopicante.blogspot.fr/2014/05/loading-matlab-mat-file-into-pandas.html
- Finally you can use PyHogs but still use scipy:
Reading complex
.mat
files.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屋!