'How can I convert a .mat file into a pandas dataFrame (via a dictionary)?
when I try to convert a .mat file, I encounter the following error:
ValueError: Data must be 1-dimensional
This is the file: https://drive.google.com/file/d/1sWxsEVX2qa14_UEVLIzeZHE4EmMU0gf9/view?usp=sharing
This is my code:
from scipy.io import loadmat
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = scipy.io.loadmat('sp1s_aa.mat')
df = pd.dataFrame(data=mat)
I understand I must convert the file data into a dictionary first, then make it a dataframe but I do not know how to do that.
Any help is greatly appreciated.
Solution 1:[1]
#Import the scipy.io.loadmat module
from scipy.io import loadmat
annots = loadmat('sp1s_aa.mat')
print(annots)
#Parse the .mat file structure
con_list = [[element for element in upperElement] for upperElement in annots['x_train']]
#Use Pandas dataframes to work with the data
import pandas as pd
newData = list(zip(con_list[0], con_list[1]))
columns = ['obj_contour_x', 'obj_contour_y']
df = pd.DataFrame(newData, columns=columns)
print(df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gaston Alex |
