Python Forum
Python dict to pandas df - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Python dict to pandas df (/thread-28337.html)



Python dict to pandas df - waleed3011 - Jul-14-2020

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:

{'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}
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]


I have tried something like this, where I called the dictionary the API provides dataToday:

tickerlist = ['YAR','DNB','NHY']
df = pd.DataFrame(columns=tickerlist)

for ticker in tickerlist:
    df = df.append(pd.DataFrame.from_dict(dataToday[ticker]))
But this gives me a data frame which looks like this:

[Image: R3NU2.png]

I know it might be a stupid or a easy question, all help is appreciated. Thanks! :)


RE: Python dict to pandas df - scidam - Jul-17-2020

The first code block doesn't represent a valid Python dictionary ({'YAR': last
date ...). Could you provide a valid sample of dataToday dictionary; It is hard to answer the question without this knowledge.
Is it looks like the following?:
dataToday = {'YAR': {'date': [val1, val2, val3], 'last': [val1, val2, val2]}, 'DNB':...}



RE: Python dict to pandas df - waleed3011 - Jul-17-2020

Is it possible to fix the dict? That is the way the financial data API provides the data... :/


RE: Python dict to pandas df - scidam - Jul-18-2020

It is likely that the API returns a json string (not a dict). Further, this json string is converted to a dictionary (in Python).The first code block (in your previous post) is not a valid Python dictionary; neither a valid json string?! So, I don't understand how your data is
presented. If dataToday is a dictionary, could you post output of print(dataToday)?