1

I am downloading data using python package yfinance and below is the code I am using for this:

# pip install yfinance

import yfinance as yf

# list of tickers for which data is to be downloaded
myLst = ['TSLA', 'MSFT', 'FB', 'IBM', 'AAPL', 'WFC', 'BAC', 'INTC', 'PRLAX', 'QASGX', 'HISFX']

# from which date?
start_date = '2018-01-01'

# to which date?
end_date = '2019-12-31'

# download the data
mydf = yf.download(myLst, start = start_date, end = end_date, group_by = "ticker")

This returns me the data in a dataframe which looks like this:

enter image description here

But I am not able to get this data into a dataframe like this:

enter image description here

Although I tried something like this :

assets = ['IBM', 'MSFT', 'FB']

assetsDF = pd.DataFrame({
    a: {x['Close'] for x in mydf[a]['Close']} for a in assets
})

But I know I am not correct here. Is there any solution?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
LeMarque
  • 733
  • 5
  • 21
  • Does this answer your question? [Pandas column multi-index to rows](https://stackoverflow.com/questions/63106554/pandas-column-multi-index-to-rows) – Trenton McKinney Aug 03 '20 at 23:25
  • That was to link to other questions that are similar. See Linked and Related on the right side of the screen – Trenton McKinney Aug 05 '20 at 17:52
  • 1
    You will probably find [How to deal with multi-level column names downloaded with yfinance?](https://stackoverflow.com/questions/63107594) helpful – Trenton McKinney Aug 05 '20 at 17:54

1 Answers1

1

As answered here: pandas dataframe select columns in multiindex

mydf.iloc[:, mydf.columns.get_level_values(1)=='Close']

@EDIT: remove useless column level

mydf.columns = mydf.columns.droplevel(1)
ce.teuf
  • 746
  • 6
  • 13
  • thanks so much @ct30 , but this still does not return the required format. why there is one more row 'Close'? What I am looking is col headers with just date and names of the tickers in assets list. :) – LeMarque Apr 12 '20 at 10:27