Effacer les filtres
Effacer les filtres

How do i convert a Python Pandas Dataframe to Matlab types?

243 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 3 Juin 2024
Starting in R2024a, you can convert a pandas DataFrame to a MATLAB table using the table function. You can also convert a pandas DataFrame to a MATLAB timetable using the timetable function. In addition, you can convert a MATLAB table or timetable to a pandas DataFrame using the py.pandas.DataFrame function. You can also pass MATLAB tables or timetables directly to Python functions without converting them to pandas DataFrames first.
For more information and examples see:
.

Plus de réponses (6)

Todd Leonhardt
Todd Leonhardt le 18 Mai 2016
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

Todd Leonhardt
Todd Leonhardt le 18 Mai 2016
Modifié(e) : Todd Leonhardt le 18 Mai 2016
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

Jeffrey Daniels
Jeffrey Daniels le 19 Oct 2018
Modifié(e) : Jeffrey Daniels le 1 Nov 2019
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 commentaire
Dev-iL
Dev-iL le 6 Mai 2020
Would it be possible for you to post a code sample? There are several moving parts in this solution...

Connectez-vous pour commenter.


Artem Lensky
Artem Lensky le 25 Mai 2022
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
Bryan Yu le 1 Déc 2017
HDF5 is native for both pandas and MATLAB. Could be a good compromise for speed and type safety.

Francesco Giaretta
Francesco Giaretta le 1 Fév 2024

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by