如何将具有多列和索引的JSON字符串转换为熊猫数据帧?

我需要将具有2列和索引的JSON字符串转换为pandas数据帧,但我遇到错误,找不到解决方案。


我试图用不同的方向对JSON进行编码,我还规范化了JSON字符串并指定了列名,但仍然存在错误。看起来有点棘手,我该怎么做?orient='columnsorient='index


这是我使用的字符串:


> type(data)

<class 'str'>


> print(data)

{

"2020-04-02T00:00:00.000Z": {

    "A": 133.25,

    "B": 0.000155642

},

"2020-04-03T00:00:00.000Z": {

    "A": 136.45,

    "B": 0.0001498913

},

"2020-04-04T00:00:00.000Z": {

    "A": 141.55,

    "B": 0.0001471562

}

}


df = pd.DataFrame.from_dict(data)

df = pd.DataFrame.from_dict(json_normalize(data), orient='index', columns=['A', 'B'])

蟒蛇抛出一个错误,说.AttributeError: 'str' object has no attribute 'values'


我想要的是这样的数据帧:


                                A         B

2020-04-02 00:00:00+00:00  133.25  0.000156

2020-04-03 00:00:00+00:00  136.45  0.000150

2020-04-04 00:00:00+00:00  141.55  0.000147

谢谢


慕仙森
浏览 63回答 1
1回答

慕慕森

您可以使用pandas.read_json。从文档中,您可以指定 = 这是字典中预期 JSON 字符串格式的指示,例如orientindex{index -> {column -> value}}>>> import pandas as pd>>> data = """... {...&nbsp; &nbsp; &nbsp;"2020-04-02T00:00:00.000Z": {...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"A": 133.25,...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"B": 0.000155642...&nbsp; &nbsp; &nbsp;},...&nbsp; &nbsp; &nbsp;"2020-04-03T00:00:00.000Z": {...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"A": 136.45,...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"B": 0.0001498913...&nbsp; &nbsp; &nbsp;},...&nbsp; &nbsp; &nbsp;"2020-04-04T00:00:00.000Z": {...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"A": 141.55,...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"B": 0.0001471562...&nbsp; &nbsp; &nbsp;}... }""">>>&nbsp;>>> df = pd.read_json(data, orient='index')>>> df&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;B2020-04-02 00:00:00+00:00&nbsp; 133.25&nbsp; 0.0001562020-04-03 00:00:00+00:00&nbsp; 136.45&nbsp; 0.0001502020-04-04 00:00:00+00:00&nbsp; 141.55&nbsp; 0.000147
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python