How can I use Python pandas in MATLAB?
Afficher commentaires plus anciens
Looking at this tutorial (https://uk.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html), it appears easy to use a Python module in MATLAB.
I'd like to execute the following Python code snippet in MATLAB:
import pandas
tbl = pandas.read_csv('xxxxx.csv')
However, the following does not work in MATLAB.
tbl = py.pandas.read_csv('xxxxx.csv')
Any suggestions?
Réponse acceptée
Plus de réponses (1)
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. Here is one example
Name = {["Roger", "Sanchez"];
["Paul", "Johnson"];
["Lisa", "Li"];
["Don", "Diaz"];
["Havana ", "Brown"]};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Name,Age,Smoker,Height,Weight,BloodPressure);
T.BMI = (T.Weight * 0.453592)./(T.Height * 0.0254).^2;
df = t2df(T); % Convert Table to Python Pandas
% Sample from the dataframe
df_sampled = df.sample(int64(10), replace=true); % Call DataFrame functions
table_sampled = df2t(df_sampled) % Convert DataFrame back to Table
Catégories
En savoir plus sur Call Python from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!