ValueError:数组必须全长相同

时间:2018-05-25 14:19:54

标签: python pandas dataframe

我正在尝试使用此API请求的结果创建数据框: Link

json_url = https://poloniex.com/public?command=returnLoanOrders&currency=ETH
df = pd.read_json(json_url)

我收到此错误消息:

  

ValueError:数组必须全长相同

我该如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

您收到错误是因为您尝试读入的json不在pandas可以使用该方法处理的结构中。

如果您执行以下操作,则很容易看到。

import requests
import json
import pandas 

json_url = 'https://poloniex.com/public?command=returnLoanOrders&currency=ETH'

# download the data
j = requests.get(url=json_url)

# looks to be json so let's see if json will load it
content = json.loads(j.content)

# we are in luck. but it looks like there is a dictionary with two keys 
# holding a list of dictionaries. The reason for the error is seen here that 
# "offers" has a bunch of records but "demands" has none. '''

content output:

{'offers': [{'rate': '0.00000100', 'amount': '12672.62879927', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00000110', 'amount': '27.16815566', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000117', 'amount': '1.00024674', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000120', 'amount': '1.50427404', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000140', 'amount': '7.54954484', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000150', 'amount': '367.48768372', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000190', 'amount': '16.84638373', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000200', 'amount': '3912.00271815', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00000299', 'amount': '5.00084825', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000300', 'amount': '291.15558822', 'rangeMin': 2, 'rangeMax': 10}, {'rate': '0.00000400', 'amount': '25.90539384', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000490', 'amount': '5.00000000', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000500', 'amount': '65.71144749', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000600', 'amount': '7.88166419', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000700', 'amount': '6.02013828', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00000800', 'amount': '36.45995800', 'rangeMin': 2, 'rangeMax': 10}, {'rate': '0.00000900', 'amount': '24.66358628', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00001000', 'amount': '340.04395302', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00001100', 'amount': '4.00000000', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00001200', 'amount': '1.00934388', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00001300', 'amount': '2.20384923', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00001400', 'amount': '1.30454381', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00001500', 'amount': '17.39350331', 'rangeMin': 2, 'rangeMax': 10}, {'rate': '0.00001800', 'amount': '9.83939462', 'rangeMin': 2, 'rangeMax': 5}, {'rate': '0.00001900', 'amount': '3.91698447', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00002000', 'amount': '16.08173442', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00002500', 'amount': '8.41234098', 'rangeMin': 2, 'rangeMax': 10}, {'rate': '0.00002600', 'amount': '1.00000000', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00002700', 'amount': '159.15504725', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00002900', 'amount': '6.55415064', 'rangeMin': 2, 'rangeMax': 5}, {'rate': '0.00003000', 'amount': '13.68180710', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00003006', 'amount': '1.09936620', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00003010', 'amount': '1.01000010', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00003100', 'amount': '1.52393451', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00003200', 'amount': '144.54550640', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00004000', 'amount': '71.89999417', 'rangeMin': 2, 'rangeMax': 10}, {'rate': '0.00004600', 'amount': '2.88401478', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00004900', 'amount': '6.57440554', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00005000', 'amount': '46.10174697', 'rangeMin': 2, 'rangeMax': 20}, {'rate': '0.00005500', 'amount': '33.07841354', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00006000', 'amount': '64.99056787', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00006400', 'amount': '7.55273677', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00006500', 'amount': '55.10000000', 'rangeMin': 2, 'rangeMax': 60}, {'rate': '0.00006900', 'amount': '1.12622772', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00007000', 'amount': '1.90111490', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00007500', 'amount': '5.00000000', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00007800', 'amount': '67.96888619', 'rangeMin': 2, 'rangeMax': 20}, {'rate': '0.00008000', 'amount': '11.83796201', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00008058', 'amount': '2.15488827', 'rangeMin': 2, 'rangeMax': 2}, {'rate': '0.00008100', 'amount': '1.22116406', 'rangeMin': 10, 'rangeMax': 10}], 'demands': []}


# we can read "offers" in to a dataframe
df = pd.DataFrame.from_dict(content['offers']

df.head()
           amount  rangeMax  rangeMin        rate
0  12672.62879927        60         2  0.00000100
1     27.16815566         2         2  0.00000110
2      1.00024674         2         2  0.00000117
3      1.50427404         2         2  0.00000120
4      7.54954484         2         2  0.00000140