首页 > 解决方案 > 将json数据存储到熊猫中

问题描述

在这里,我正在定义我的 pandas DF

df = pd.DataFrame(columns=['ColA',
                       'ColB',
                       'ColC',
                       'ColD'
                          ]

然后使用我想要的数据对 json 响应进行休息请求

..building json requst
js = request.json()
return_list = js["result"]["data"] #json list

然后我使用它来将该数据添加到我的数据框中

#loop through returned data
for id in return_list :
    line = []
    #looping through all elements for each data point
    for x in return_list[id]:
        line.append(torrents[id][x])
    df.loc[df.size] = line

但是,最后的代码块非常慢..我该如何优化它?

这是一个如果可行的话我可以做什么的例子

df = pd.DataFrame(columns=['a',
                       'b',
                       'c',
                       'd'
              ])

x = [[1,2,3,4],[1,2,3,4]]
df.append(x, ignore_index=True)
#however, df will be empty

标签: pythonpandas

解决方案


推荐阅读