Using Python to replace MATLAB: how to import data?


Question

I want to use some Python libraries to replace MATLAB. How could I import Excel data in Python (for example using NumPy) to use them?

I don't know if Python is a credible alternative to MATLAB, but I want to try it. Is there a a tutorial?

1
11
8/22/2013 6:52:28 PM

Accepted Answer

Depending on what kind of computations you are doing with MATLAB (and on which toolboxes you are using), Python could be a good alternative to MATLAB.

Python + NumPy + SciPy + Matplotlib are the right combination to start.

For the data, you can, for example, save your data directly in text file (assuming that you are not directly concerned by floating-point precision issues) and read it in Python.

If your data are Excel data, where each value is separated by a ";", you can for example read the file line by line, and use the split() method (with ";" as argument) to get each value.

For MATLAB up to version 7.1, it is possible to directly load .mat files from Python with the scipy.io.matlab.mio module.

12
8/22/2013 6:54:21 PM

Pandas is a Python data analysis library that can import/export from Excel pretty easily. Here's how to do it:

http://pandas.pydata.org/pandas-docs/stable/10min.html#excel

Crash course:

import pandas as pd
data = pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon