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


Messages In This Thread
Python dict to pandas df - by waleed3011 - Jul-14-2020, 09:56 PM
RE: Python dict to pandas df - by scidam - Jul-17-2020, 02:00 AM
RE: Python dict to pandas df - by waleed3011 - Jul-17-2020, 05:17 PM
RE: Python dict to pandas df - by scidam - Jul-18-2020, 04:08 AM

Forum Jump:

User Panel Messages

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