似乎无法弄清楚为什么我无法将JSON读入DataFrame

时间:2017-06-10 03:24:01

标签: python json pandas

如何将此JSON的“数据”部分转换为简单的pandas表。这就是我所拥有的:

CoinName,Algorithm,...
ABC,          AAA, ...
DEF,          DDS, ...

尝试了很多方法,但结果看起来并不正确。该表应为:

{{1}}

3 个答案:

答案 0 :(得分:2)

对我好看

pandas.DataFrame(json_obj['Data']).T

          Algorithm          CoinName                 FullName FullyPremined  
007          Scrypt          007 coin           007 coin (007)             0   
1337            X13              1337              1337 (1337)             0   
1CR          Scrypt           1Credit            1Credit (1CR)             0   
1ST             N/A        FirstBlood         FirstBlood (1ST)             1   
2015            X11         2015 coin         2015 coin (2015)             0   
2BACCO       Scrypt       2BACCO Coin     2BACCO Coin (2BACCO)             0   
2GIVE        Scrypt         2GiveCoin        2GiveCoin (2GIVE)             0   
32BIT           X11         32Bitcoin        32Bitcoin (32BIT)             0   
365             X11           365Coin            365Coin (365)             0   
404          Scrypt           404Coin            404Coin (404)             0   
42           Scrypt           42 Coin             42 Coin (42)             0   
611          SHA256         SixEleven          SixEleven (611)             0   
808          SHA256               808                808 (808)             0   
888             N/A          Octocoin           Octocoin (888)             0   

答案 1 :(得分:0)

不完整的答案,但我没有发表评论的声誉。

urllib.request.urlopen正在返回一个对象,但是json.load期待一个字符串。

如果您正在寻找字符串,则必须使用错误的方法。 https://docs.python.org/3.4/library/urllib.request.html?highlight=urllib.request.urlopen#urllib.request.urlopen

>>> json_obj = json.load(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File  "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'    
>>> url.info()
<http.client.HTTPMessage object at 0x105ab3518>    
>>> url.getcode()
200

答案 2 :(得分:0)

一旦您阅读了JSON,您现在实际上在dict中拥有了一个Python json_data。因此,您需要pandas.DataFrame.from_dict

df = pandas.DataFrame.from_dict(json_obj['Data'], orient='index')