Machi
Machi

Reputation: 131

How to get a nested dictionary inside a list into Dataframe?

I'm trying to get data from an exchange's api, and it is in a list with dict inside, I think? So I want to create a table, with the name, initial price, price, high, low and so on.. question: How do I get them into the dataframe format? Index with pd.Dataframe works but only if it's one stock.

pd.Series is the best I got to so far and looks like this:

1 {'BTC-ACM': {'initialprice': '0.00000390', 'pr...
0 {'BTC-AEON': {'initialprice': '0.00010617', 'p...

Raw Data look like this:

[{'BTC-ACM': {'initialprice': '0.00000380', 'price': '0.00000428', 'high': 
'0.00000510', 'low': '0.00000351', 'volume': '0.24647930', '
 bid': '0.00000433', 'ask': '0.00000465'}}, {'BTC-AEON': {'initialprice': 
'0.00010652', 'price': '0.00011040', 'high': '0.00013774', 'lo
 w': '0.00010616', 'volume': '1.17486173', 'bid': '0.00011040', 'ask': 
'0.00011867'}},....]

Attempt to make it a table like this:

Name   | InitialPrice |Price |High |Low|
BTC-ACM| 0.0000132    |0.123 |0.9  |0.2|
BTC-AEO| 0.2131243    |0.213 |0.2  |0.1|

Upvotes: 1

Views: 53

Answers (1)

cs95
cs95

Reputation: 402353

Reshape your data and then call DataFrame.to_dict:

df = pd.DataFrame.from_dict(
    {k : d[k] for d in data for k in d}, orient='index')

df[['initialprice', 'price', 'high', 'low']]

         initialprice       price        high         low
BTC-ACM    0.00000380  0.00000428  0.00000510  0.00000351
BTC-AEON   0.00010652  0.00011040  0.00013774  0.00010616

Upvotes: 1

Related Questions