Search code examples
pythonpandaspycharmyahoo-finance

Getting a specific element from a Pandas Dataframe


This is probably a rookie question, but I haven't been able to find the solution.

I'm trying to collect some data from Yahoo Finance using pandas.

from pandas_datareader import data

tickers = ['EQNR.OL','BP','CL=F']
start_date = '2001-01-02'
end_date = '2021-02-26'
panel_data = data.DataReader(tickers, 'yahoo', start_date, end_date)

I wanna take a look at the BP stock (I need all 3, so excluding EQNR.OL and CL=F from tickers is not the right solution). I know how to get all the close prices of a single stock: close_BP = panel_data['Close','BP']

But is there a way I can get all BP data (open, close, high, low) withdrawn from 'panel_data', and not only a specific column like 'close'? I was thinking something like BP = panel_data[:,'BP'] or BP = panel_data.loc[:,'BP'] but it doesn't work.

A big thanks in advance.


Solution

  • I think what you're looking for is pandas.IndexSlice...

    import pandas as pd
    panel_data.loc[:, pd.IndexSlice[:, 'BP']]