1

How do I put the following json output into a pandas dataframe?

[{'currency': '1ST', 'available': '0', 'reserved': '0'}, {'currency': '8BT', 'available': '0', 'reserved': '0'}, {'currency': 'ADX', 'available': '0', 'reserved': '0'}, {'currency': 'AE', 'available': '0', 'reserved': '0'}, {'currency': 'AEON', 'available': '0', 'reserved': '0'}, {'currency': 'AIR', 'available': '0', 'reserved': '0'}, {'currency': 'AMB', 'available': '0', 'reserved': '0'}, {'currency': 'AMM', 'available': '0', 'reserved': '0'}, {'currency': 'AMP', 'available': '0', 'reserved': '0'}]

I've tried the following but only receive the following error:

Code

balances = pd.read_json(data)

Error

ValueError: Invalid file path or buffer object type: <class 'method'>

Edit - How I get the data:

def get_account_balance(self):
    """Get main balance."""
    return self.session.get("https://api.hitbtc.com/api/2/account/balance").json()
alpenmilch411
  • 483
  • 1
  • 5
  • 18

4 Answers4

4

IIUC assuming you have a dictionary:

In [231]: d = [{'currency': 'ZRX', 'available': '0', 'reserved': '0'}, {'currency': 'ZSC', 'available': '0', 'reserved': '0'}]

In [235]: pd.DataFrame(d)
Out[235]:
  available currency reserved
0         0      ZRX        0
1         0      ZSC        0

If it's a string (broken JSON, as JSON must have double quotes instead of single quotes):

import json

In [238]: s = """
     ...: [{'currency': 'ZRX', 'available': '0', 'reserved': '0'}, {'currency': 'ZSC', 'available': '0', 'reserved': '0'}]
     ...: """

In [239]: d = json.loads(s.replace("'", '"'))

In [240]: pd.DataFrame(d)
Out[240]:
  available currency reserved
0         0      ZRX        0
1         0      ZSC        0
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
1

The following worked for me:

pd.DataFrame.from_dict(data, orient='columns')

Source: https://github.com/vi3k6i5/pandas_basics/blob/master/1_a_create_a_dataframe_from_dictonary.ipynb

alpenmilch411
  • 483
  • 1
  • 5
  • 18
1

I had the same problem while importing a json file to a pandas dataframe. I've solved it by flattening the dictionaries and then importing them normally:

def get_values(lVals):
    res = []
    for val in lVals:
        if type(val) not in [list, set, tuple]:
            res.append(val)
        else:
            res.extend(get_values(val))
    return res

data = get_values(data)

df = pd.DataFrame(data)
tmsss
  • 1,979
  • 19
  • 23
0

Reference:

Python how convert single quotes to double quotes to format as json string | user3850

import json
import pandas as pd

json_data = [{'currency': '1ST', 'available': '0', 'reserved': '0'}, {'currency': '8BT', 'available': '0', 'reserved': '0'}, {'currency': 'ADX', 'available': '0', 'reserved': '0'}, {'currency': 'AE', 'available': '0', 'reserved': '0'}, {'currency': 'AEON', 'available': '0', 'reserved': '0'}, {'currency': 'AIR', 'available': '0', 'reserved': '0'}, {'currency': 'AMB', 'available': '0', 'reserved': '0'}, {'currency': 'AMM', 'available': '0', 'reserved': '0'}, {'currency': 'AMP', 'available': '0', 'reserved': '0'}]

json_format = json.dumps(json_data)

json_df = pd.read_json(json_format)

json_df.head()
datalifenyc
  • 2,028
  • 1
  • 20
  • 18