猿问

提取部分 json 键值并组合

我有这个 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网站


青春有我
浏览 129回答 3
3回答

慕田峪9158850

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 pddf = pd.DataFrame(data, columns = data['dataset']['column_names'])for i, data_row in enumerate(data['dataset']['data']):    df.loc[i] = data_row

慕娘9325324

以下代码段应该适合您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

慕妹3242003

cols = data['dataset']['column_names']data = data['dataset']['data']这很简单labeled_data = [dict(zip(cols, d)) for d in data]
随时随地看视频慕课网APP

相关分类

Python
我要回答