将Json数据转换为Python DataFrame [英] Convert Json data to Python DataFrame
问题描述
这是我第一次访问API/使用json数据,因此,如果有人可以将我引向了解如何使用它的良好资源,我将非常感激.
This is my first time accessing an API / working with json data so if anyone can point me towards a good resource for understanding how to work with it I'd really appreciate it.
不过,具体来说,我具有以下形式的json数据:
Specifically though, I have json data in this form:
{"result": { "code": "OK", "msg": "" },"report_name":"DAILY","columns":["ad","ad_impressions","cpm_cost_per_ad","cost"],"data":[{"row":["CP_CARS10_LR_774470","966","6.002019","5.797950"]}],"total":["966","6.002019","5.797950"],"row_count":1}
我了解这种结构,但是我不知道如何正确地将其放入DataFrame.
I understand this structure but I don't know how to get it into a DataFrame properly.
推荐答案
看看您的json
的结构,大概您的数据将有几行,我认为自己构建数据框会更有意义
Looking at the structure of your json
, presumably you will have several rows for your data and in my opinion it will make more sense to build the dataframe yourself.
此代码使用columns
和data
来构建数据框:
This code uses columns
and data
to build a dataframe:
In [12]:
import json
import pandas as pd
with open('... path to your json file ...') as fp:
for line in fp:
obj = json.loads(line)
columns = obj['columns']
data = obj['data']
l = []
for d in data:
l += [d['row']]
df = pd.DataFrame(l, index=None, columns=columns)
df
Out[12]:
ad ad_impressions cpm_cost_per_ad cost
0 CP_CARS10_LR_774470 966 6.002019 5.797950
对于其余数据,在您的json
中,我想您可以用总计来检查您的数据框,
As for the rest of the data, in your json
, I guess you could e.g. use the totals for checking your dataframe,
In [14]:
sums = df.sum(axis=0)
obj['total']
for i in range(0,3):
if (obj['total'][i] != sums[i+1]):
print "error in total"
In [15]:
if obj['row_count'] != len(df.index):
print "error in row count"
对于json
中的其余数据,我很难知道是否还应该做其他事情.
As for the rest of the data in the json
, it is difficult for me to know if anything else should be done.
希望有帮助.
这篇关于将Json数据转换为Python DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!