MATLAB Answers

How can I use Python pandas in MATLAB?

206 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

Kouichi C. Nakamura
Kouichi C. Nakamura on 22 May 2019
Edited: Kouichi C. Nakamura on 23 May 2019
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 Comments

Dominik Mattioli
Dominik Mattioli on 13 Jun 2019
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 on 23 Jun 2020
It probably worked because the default Anaconda environment ("base") has a huge number of modules (including pandas) installed.

Sign in to comment.

More Answers (0)


Translated by