Access data in a nested df

77 Views Asked by At

I am using a library to get stock data. The data is then put into a df but in a nested form. I want to get data from two specific columns but the date column key is not recognised.

import numpy as np
from yahooquery import Ticker

mydata = Ticker('WMT', asynchronous=True)
# the above returns a yahoo.ticker.Ticker object)

df = mydata.history(period = '1m', interval = '1d'
# this creates a df with daily closing info for the month

The output looks like enter image description here

If then look at the keys using df.keys() I get the following: Index(['low', 'close', 'high', 'open', 'volume', 'adjclose', 'dividends'], dtype='object')

I want to be able to have a flat df with two columns - date and close. How do I get that result? how do I even access the data in the 'date' column?

I am guessing the Ticker object is in json form but how can I tell? Unfortunately pd.json_normal(mydata) gives a 'NotImplementedError'.

The yahooquery library can be found at https://pypi.org/project/yahooquery/

Thanks in advance

1

There are 1 best solutions below

0
Imperial_J On

I tried it out, and I believe you can use pandas.DataFrame.reset_index

Code:

df.reset_index()[["date","close"]]

Output:

enter image description here