Python Forum
Python dict to pandas df
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python dict to pandas df
#1
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! :)
Reply
#2
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':...}
Reply
#3
Is it possible to fix the dict? That is the way the financial data API provides the data... :/
Reply
#4
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)?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020