Storing multi index data frame in Matlab for data analysis and processing

9 ビュー (過去 30 日間)
Prashant joshi
Prashant joshi 2025 年 1 月 6 日
コメント済み: Prashant joshi 2025 年 1 月 6 日
I run the following code wrapping yfinance a
% Define the stock tickers
tickers = {'AAPL', 'MSFT', 'GOOGL'}; % Apple, Microsoft, Google
% Set the start and end dates for the data
start_date = '2020-01-01';
end_date = '2021-01-01';
% Import the yfinance module
yfinance = py.importlib.import_module('yfinance');
Error using <frozen importlib>_find_and_load_unlocked (line 1004)
Python Error: ModuleNotFoundError: No module named 'yfinance'

Error in <frozen importlib>_find_and_load (line 1027)

Error in <frozen importlib>_gcd_import (line 1050)

Error in __init__>import_module (line 126)
% Download the data for all tickers at once
data = yfinance.download(py.list(tickers), start_date, end_date);
disp(data)
and my output looks like this:
Python DataFrame with properties:
T: [1×1 py.pandas.core.frame.DataFrame]
at: [1×1 py.pandas.core.indexing._AtIndexer]
attrs: [1×1 py.dict]
axes: [1×2 py.list]
columns: [1×1 py.pandas.core.indexes.multi.MultiIndex]
dtypes: [1×1 py.pandas.core.series.Series]
empty: 0
flags: [1×1 py.pandas.core.flags.Flags]
iat: [1×1 py.pandas.core.indexing._iAtIndexer]
iloc: [1×1 py.pandas.core.indexing._iLocIndexer]
index: [1×1 py.pandas.core.indexes.datetimes.DatetimeIndex]
loc: [1×1 py.pandas.core.indexing._LocIndexer]
ndim: [1×1 py.int]
shape: [1×2 py.tuple]
size: [1×1 py.int]
values: [1×1 py.numpy.ndarray]
Price Close ... Volume
Ticker AAPL GOOGL MSFT ... AAPL GOOGL MSFT
Date ...
2020-01-02 72.796005 68.186821 153.938202 ... 135480400 27278000 22622100
2020-01-03 72.088303 67.830101 152.021408 ... 146322800 23408000 21116200
2020-01-06 72.662704 69.638054 152.414352 ... 118387200 46768000 20813700
2020-01-07 72.320969 69.503548 151.024643 ... 108872000 34330000 21634100
2020-01-08 73.484344 69.998253 153.430283 ... 132079200 35314000 27746500
... ... ... ... ... ... ... ...
2020-12-24 129.047501 86.394814 215.745163 ... 54930100 9312000 10550600
2020-12-28 133.663040 88.377617 217.885666 ... 124486200 27650000 17933500
2020-12-29 131.883301 87.570557 217.101135 ... 121047300 19726000 17403200
2020-12-30 130.758759 86.498932 214.708817 ... 96452100 21026000 20272300
2020-12-31 129.751556 87.315475 215.425537 ... 99116600 21070000 20942100
[253 rows x 15 columns]
I wanted to save only Closing price of all tickers in Matlab, maintaining the Date as index for further processing. Can anyone help me? It will be appreciated.

回答 (1 件)

Jacob Mathew
Jacob Mathew 2025 年 1 月 6 日
Hey Prashant,
Looking through the API Documentation of yfinance's download function, it doesn't seem possible to download a specific set of columns. You can double check at the documentation link below:
However, once you have the DataFrame entirely in MATLAB, you can convert it into a MATLAB compatible format like Table to help you extract the columns that you need. You can refer to the following example to help you out:
This example can also be launched running the following command from MATLAB's Command Line:
openExample('matlab/UsePythonPandasDataFramesInMATLABExample')
  1 件のコメント
Prashant joshi
Prashant joshi 2025 年 1 月 6 日
Thanks for your reply. I appreciate it. I tried all the alternatives but could not figure it out. I tried another alternative to save it in .csv format and read the table that looks like this. They all are column headings but not the one that has with yfinance download. so not sure how to deal with.
Price Close Close_1 Close_2 High High_1 High_2 Low Low_1 Low_2 Open Open_1 Open_2 Volume Volume_1 Volume_2
__________ ______ _______ _______ ______ ______ ______ ______ ______ ______ ______ ______ ______ __________ __________ __________
NaT NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2020-01-02 72.796 68.187 153.94 72.857 68.187 154.04 71.545 67.081 151.74 71.8 67.177 152.17 1.3548e+08 2.7278e+07 2.2622e+07
2020-01-03 72.088 67.83 152.02 72.852 68.439 153.3 71.863 67.123 151.48 72.02 67.157 151.73 1.4632e+08 2.3408e+07 2.1116e+07
2020-01-06 72.663 69.638 152.41 72.701 69.663 152.48 70.954 67.306 150 71.206 67.337 150.55 1.1839e+08 4.6768e+07 2.0814e+07
2020-01-07 72.321 69.504 151.02 72.929 69.922 153.03 72.1 69.327 150.78 72.672 69.77 152.69 1.0887e+08 3.433e+07 2.1634e+07
2020-01-08 73.484 69.998 153.43 73.787 70.338 154.11 72.023 69.38 151.38 72.023 69.489 152.32 1.3208e+08 3.5314e+07 2.7746e+07

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGoogle についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by