0

I am trying to convert .mat files into csv with the following code -

from scipy.io import loadmat
import pandas as pd
data1=loadmat(r"C:\Users\ansha\Downloads\OneDrive_1_830_2022\HTS_C1_SN09_all_08.11.2019.mat")
data1={k:v for k,v in data1.items() if k[0]!="_"}
df1=pd.DataFrame({k:pd.Series(v[0])for k ,v in data1.items()})
df1.to_csv("u1.csv")

but this error keeps popping up-

File ~\anaconda3\lib\site-packages\pandas\core\series.py:405, in Series.__init__(self, data, index, dtype, name, copy, fastpath)
    401 elif isinstance(data, np.ndarray):
    402     if len(data.dtype):
    403         # GH#13296 we are dealing with a compound dtype, which
    404         #  should be treated as 2D
--> 405         raise ValueError(
    406             "Cannot construct a Series from an ndarray with "
    407             "compound dtype.  Use DataFrame instead."
    408         )
    409 elif isinstance(data, Series):
    410     if index is None:

ValueError: Cannot construct a Series from an ndarray with compound dtype.  Use DataFrame instead.

Please help me to convert .mat file to csv. Also,what does the compound dtype mean?

3
  • Welcome to SO! dtype refers to a data type object for python. Compound data types are data types that can be constructed from fundamental data types (or other compound data types). More details: learncpp.com/cpp-tutorial/introduction-to-compound-data-types Commented Aug 30, 2022 at 10:07
  • 2
    Looks like the error is due to the structure of your .mat file. If you are looking for a specific solution you should share the .mat file or explain it at least. Commented Aug 30, 2022 at 10:08
  • Does this answer your question? How to convert .mat file into csv using python? Commented Aug 30, 2022 at 18:33

1 Answer 1

0

What about data frames? Save the new data to a new vector with data["mat file"] and check the data structure with pd.DataFrame() so you can use it to create a CSV file while checking the information with the variable.to_scv("pathname") code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.