How can I use Python pandas in MATLAB?

조회 수: 80 (최근 30일)
Kouichi C. Nakamura
Kouichi C. Nakamura 2019년 5월 22일
답변: Artem Lensky 2022년 5월 25일
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?

채택된 답변

Kouichi C. Nakamura
Kouichi C. Nakamura 2019년 5월 22일
편집: Kouichi C. Nakamura 2019년 5월 23일
pyversion returns Python installation outside of Anaconda.
>> pyversion
version: '2.7'
executable: 'C:\Python27\python.EXE'
library: 'C:\WINDOWS\system32\python27.dll'
home: 'C:\Python27'
isloaded: 1
So I needed to change the reference to the Anaconda version of Python.
After relaunching MATLAB, I used pyversion again to change the Python path. Then py.pandas.read_csv() worked!
>> pyversion 'C:\Users\xxxxxxxxx\AppData\Local\Continuum\anaconda2\python.EXE'
>> pyversion
version: '2.7'
executable: 'C:\Users\xxxxxxxxx\AppData\Local\Continuum\anaconda2\python.EXE'
library: 'C:\Users\xxxxxxxxx\AppData\Local\Continuum\anaconda2\python27.dll'
home: 'C:\Users\xxxxxxxxx\AppData\Local\Continuum\anaconda2'
isloaded: 1
>> py.pandas.read_csv('xxxxxxxxx.csv')
ans =
Python DataFrame with properties:
T: [1×1 py.pandas.core.frame.DataFrame]
at: [1×1 py.pandas.core.indexing._AtIndexer]
axes: [1×2 py.list]
blocks: [1×1 py.dict]
columns: [1×1 py.pandas.core.indexes.base.Index]
empty: 0
iat: [1×1 py.pandas.core.indexing._iAtIndexer]
iloc: [1×1 py.pandas.core.indexing._iLocIndexer]
index: [1×1 py.pandas.core.indexes.range.RangeIndex]
is_copy: [1×1 py.NoneType]
ix: [1×1 py.pandas.core.indexing._IXIndexer]
loc: [1×1 py.pandas.core.indexing._LocIndexer]
ndim: 2
shape: [1×2 py.tuple]
size: 120
style: [1×1 py.pandas.io.formats.style.Styler]
values: [1×1 py.numpy.ndarray]
...
0 ...
1 ...
2 ...
3 ...
4 ...
5 ...
6 ...
7 ...
8 ...
9 ...
10 ...
11 ...
12 ...
13 ...
14 ...
[15 rows x 8 columns]
  댓글 수: 2
Dominik Mattioli
Dominik Mattioli 2019년 6월 13일
You didn't need to point python to your anaconda environment too? Particularly, where the environment in which pandas is installed?
Brett Swanson
Brett Swanson 2020년 6월 23일
It probably worked because the default Anaconda environment ("base") has a huge number of modules (including pandas) installed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Artem Lensky
Artem Lensky 2022년 5월 25일
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

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by