【发布时间】:2020-02-29 19:24:37
【问题描述】:
我想将 JSON 转换为 Pandas 数据框。
我的 JSON 看起来像: 喜欢:
{
"country1":{
"AdUnit1":{
"floor_price1":{
"feature1":1111,
"feature2":1112
},
"floor_price2":{
"feature1":1121
}
},
"AdUnit2":{
"floor_price1":{
"feature1":1211
},
"floor_price2":{
"feature1":1221
}
}
},
"country2":{
"AdUnit1":{
"floor_price1":{
"feature1":2111,
"feature2":2112
}
}
}
}
我使用以下代码从 GCP 读取文件:
project = Context.default().project_id
sample_bucket_name = 'my_bucket'
sample_bucket_path = 'gs://' + sample_bucket_name
print('Object: ' + sample_bucket_path + '/json_output.json')
sample_bucket = storage.Bucket(sample_bucket_name)
sample_bucket.create()
sample_bucket.exists()
sample_object = sample_bucket.object('json_output.json')
list(sample_bucket.objects())
json = sample_object.read_stream()
我的目标是获得如下所示的 Pandas 数据框:
我尝试使用json_normalize,但没有成功。
【问题讨论】:
-
pd.read_json怎么样? -
我试过了,结果不好:c2n.me/44pYvfb
-
看看this的回答,因为我认为你需要先将json“展平”才能使用
pd.read_json(json.dumps(json_dictionary))
标签: python json pandas dataframe