Ask Your Question
2

What is the solution for handling column names with multiple levels that are obtained through yfinance download?

asked 2021-12-03 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-09-01 09:00:00 +0000

bukephalos gravatar image

One possible solution for handling column names with multiple levels in yfinance data download is to use pandas' MultiIndex feature. This allows us to handle hierarchical indices and columns in a more structured and efficient manner.

To create a MultiIndex for the columns, we can use the from_tuples method and pass in a list of tuples that represent the levels of the column names. For example, to create a MultiIndex with two levels for the OHLC data, we can do the following:

import yfinance as yf

# download data for 'AAPL' stock
df = yf.download('AAPL', start='2021-01-01', end='2021-06-30')

# create multi-level columns
cols = [('Open', 'AAPL'), ('High', 'AAPL'), ('Low', 'AAPL'), ('Close', 'AAPL'), ('Adj Close', 'AAPL'), ('Volume', 'AAPL')]
df.columns = pd.MultiIndex.from_tuples(cols)

This will create a DataFrame with column names like (Open, AAPL), (High, AAPL), etc. We can then access the columns using the .loc accessor and pass in a tuple of the levels we want to select. For example, to select the 'Open' column for 'AAPL', we can do:

print(df.loc[:, ('Open', 'AAPL')])

This will output a Series with the 'Open' prices for 'AAPL'. We can also use slicing and other operations on the MultiIndex columns as needed.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-03 11:00:00 +0000

Seen: 9 times

Last updated: Sep 01 '22