How do i convert a Python Pandas Dataframe to Matlab types?
Mostrar comentarios más antiguos
I am calling a python function from Matlab code which returns a Pandas Dataframe. I would like to convert the content of the DataFrame to Matlab data types, but I can't find the correct way to do it. Thanks in advance for any help on this.
1 comentario
per isakson
el 18 de Mayo de 2016
Respuesta aceptada
Más respuestas (6)
Todd Leonhardt
el 18 de Mayo de 2016
4 votos
A simple and effective, but perhaps inelegant, solution is to first save the Pandas DataFrame as a CSV file and then read that data into MATLAB as a table datatype (as long as you have MATLAB R2013b or newer).
You can save a Pandas DataFrame as a CSV file with the DataFrame.to_csv() function: http://pandas.pydata.org/pandas-docs/version/0.18.1/generated/pandas.DataFrame.to_csv.html
You can read a CSV file into MATLAB as a table using the readtable() function: http://www.mathworks.com/help/matlab/ref/readtable.html
1 comentario
Danilo Botero Lopez
el 15 de Dic. de 2022
It worked!
Todd Leonhardt
el 18 de Mayo de 2016
Editada: Todd Leonhardt
el 18 de Mayo de 2016
2 votos
Another possibility is to convert the Pandas DataFrame to a Python dictionary using the DataFrame.to_dict() method: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html
Once you have the data as a Python Dictionary, you should be able to a use that in MATLAB as shown here: http://www.mathworks.com/help/matlab/matlab_external/use-python-dict-type-in-matlab.html
1 comentario
Danilo Botero Lopez
el 15 de Dic. de 2022
Thank you!
Jeffrey Daniels
el 19 de Oct. de 2018
Editada: Jeffrey Daniels
el 1 de Nov. de 2019
1 voto
I solved this problem by querying the Pandas dataframe column data types and inserting them as the last row of the dataframe, convert the df to string and pass to Matlab. Then once in Matlab I have a method that reads the string into a Matlab table and applies the data type specified in the last row of the CSV to each column of the table. It's not elegant, but it is fairly bulletproof. This method works completely in memory space and does not require writing to hard drive.
1 comentario
Dev-iL
el 6 de Mayo de 2020
Would it be possible for you to post a code sample? There are several moving parts in this solution...
Artem Lensky
el 25 de Mayo de 2022
1 voto
It implements two funcions:
- df2t - that converts Pandas DataFrame to Matlab Table
- t2df - that converts Matlab Table to Pandas DataFrame.
The examples are shown in test.mlx.
Bryan Yu
el 1 de Dic. de 2017
0 votos
HDF5 is native for both pandas and MATLAB. Could be a good compromise for speed and type safety.
Francesco Giaretta
el 1 de Feb. de 2024
0 votos
Starting from MATLAB R2024a, is it possible to convert a Pandas DataFrame directly into a Matlab table:
Categorías
Más información sobre Call Python from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!