为了账号安全,请及时绑定邮箱和手机立即绑定

提取部分 json 键值并组合

提取部分 json 键值并组合

青春有我 2022-06-02 15:24:20
我有这个 json 数据集。从这个数据集中,我只想要“column_names”键及其值和“data”键及其值。column_names 的每个值对应于数据值。我如何在python中只组合这两个键进行分析{"dataset":{"id":42635350,"dataset_code":"MSFT","column_names":["Date","Open","High","Low","Close","Volume","Dividend","Split", "Adj_Open","Adj_High","Adj_Low","Adj_Close","Adj_Volume"],"frequency":"daily","type":"Time Series","data":[["2017-12-28",85.9,85.93,85.55,85.72,10594344.0,0.0,1.0,83.1976157998082,83.22667201021558,82.85862667838872,83.0232785373639,10594344.0],["2017-12-27",85.65,85.98,85.215,85.71,14678025.0,0.0,1.0,82.95548071308001,83.27509902756123,82.53416566217294,83.01359313389476,14678025.0]for cnames in data['dataset']['column_names']:print(cnames)for cdata in data['dataset']['data']:print(cdata)For 循环给了我想要的列名和数据值,但我不知道如何将它组合起来,并将其作为 python 数据框进行分析。Ref:以上代码来自qudal网站
查看完整描述

3 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

data = {

  "dataset": {

      "id":42635350,"dataset_code":"MSFT",

      "column_names": ["Date","Open","High","Low","Close","Volume","Dividend","Split","Adj_Open","Adj_High","Adj_Low","Adj_Close","Adj_Volume"],

      "frequency":"daily",

      "type":"Time Series",

      "data":[

          ["2017-12-28",85.9,85.93,85.55,85.72,10594344.0,0.0,1.0,83.1976157998082, 83.22667201021558,82.85862667838872,83.0232785373639,10594344.0], 

          ["2017-12-27",85.65,85.98,85.215,85.71,14678025.0,0.0,1.0,82.95548071308001,83.27509902756123,82.53416566217294,83.01359313389476,14678025.0]

      ]

  }

}

下面的代码应该做你想做的吗?


import pandas as pd

df = pd.DataFrame(data, columns = data['dataset']['column_names'])

for i, data_row in enumerate(data['dataset']['data']):

    df.loc[i] = data_row


查看完整回答
反对 回复 2022-06-02
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

以下代码段应该适合您

import pandas as pd
df = pd.DataFrame(data['dataset']['data'],columns=data['dataset']['column_names'])

检查以下链接以了解更多信息 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html


查看完整回答
反对 回复 2022-06-02
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

cols = data['dataset']['column_names']

data = data['dataset']['data']

这很简单


labeled_data = [dict(zip(cols, d)) for d in data]


查看完整回答
反对 回复 2022-06-02
  • 3 回答
  • 0 关注
  • 128 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信