Kevin
Kevin

Reputation: 1729

Creating a Pandas DataFrame from Uneven List of Dictionaries

I am trying to get a dictionary into a formatted DataFrame.

I am getting the data through an API call from: https://www.cryptonator.com/api

r = requests.get('https://api.cryptonator.com/api/ticker/btc-usd')
x = r.json()

The data returned is in the format:

{"ticker":{"base":"BTC","target":"USD","price":"443.7807865468","volume":"31720.1493969300","change":"0.3766203596"},
"timestamp":1399490941,
"success":true,
"error":""
}

I am only interested in some of the data in the dictionary "ticker" and the key value pair, "timestamp".

My issue is that since "ticker" is a dictionary within a dictionary whereas "timestamp" is simply a key value pair, I cannot seem to import them into this specific Pandas DataFrame:

  base           price   timestamp            volume
0  BTC  443.7807865468  1399490941  31720.1493969300

I've tried several methods that don't exactly give me what I'm looking for including:

pd.DataFrame(x).T 
                 base       change          price      target          volume
ticker            BTC  -8.71469546  1532.83742332         USD  22275.83826859
timestamp  1493925662   1493925662     1493925662  1493925662      1493925662

Upvotes: 2

Views: 414

Answers (1)

piRSquared
piRSquared

Reputation: 294258

You can pull out the pieces you need

pd.DataFrame({
        'ticker': x['ticker'],
        'timestamp': x['timestamp']
    }).T

                 base        change           price      target            volume
ticker            BTC  0.3766203596  443.7807865468         USD  31720.1493969300
timestamp  1399490941    1399490941      1399490941  1399490941        1399490941

Old Answer

pd.io.json.json_normalize(x)

  error success ticker.base ticker.change    ticker.price ticker.target     ticker.volume   timestamp
0          True         BTC  0.3766203596  443.7807865468           USD  31720.1493969300  1399490941

Upvotes: 2

Related Questions