Jul-14-2020, 09:56 PM
I have a set of data that can come through a stock data API, the amount of data and how stocks is depending on users' requests. The data I receive from the API comes in as a dictionary.
Example:
Is it possible to write a for loop in Python where for every key in the dictionary it will create a new pandas data frame row with its value and date as index?
So that the dataframe looks something like this?
![[Image: kqn60.png]](https://i.stack.imgur.com/kqn60.png)
I have tried something like this, where I called the dictionary the API provides dataToday:
But this gives me a data frame which looks like this:
![[Image: R3NU2.png]](https://i.stack.imgur.com/R3NU2.png)
I know it might be a stupid or a easy question, all help is appreciated. Thanks! :)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ 'YAR' : last date 2020 - 07 - 10 336.4 2020 - 07 - 13 344.0 2020 - 07 - 14 344.3 , 'DNB' : last date 2020 - 07 - 10 129.60 2020 - 07 - 13 142.45 2020 - 07 - 14 145.50 , 'NHY' : last date 2020 - 07 - 10 27.35 2020 - 07 - 13 28.56 2020 - 07 - 14 28.50 } |
So that the dataframe looks something like this?
![[Image: kqn60.png]](https://i.stack.imgur.com/kqn60.png)
I have tried something like this, where I called the dictionary the API provides dataToday:
1 2 3 4 5 |
tickerlist = [ 'YAR' , 'DNB' , 'NHY' ] df = pd.DataFrame(columns = tickerlist) for ticker in tickerlist: df = df.append(pd.DataFrame.from_dict(dataToday[ticker])) |
![[Image: R3NU2.png]](https://i.stack.imgur.com/R3NU2.png)
I know it might be a stupid or a easy question, all help is appreciated. Thanks! :)