简体   繁体   English

在pandas中读取JSON文件

[英]Reading a JSON file in pandas

I have the following structure of the json file, I am trying to load in pandas, but all the columns are not coming as I want. 我有以下json文件的结构,我正在尝试加载pandas,但所有的列都不是我想要的。

[
    {
        "prime": {
            "n": "0"
        },
        "min": {
            "n": "1"
        },
        "sk": {
            "s": "1#2017-02-14#19:46:00#THIRDPARTYNEW"
        },
        "price": {
            "n": "3.49"
        },
        "asin": {
            "s": "B00LEACCKG"
        },
        "shCost": {
            "n": "0"
        },
        "date": {
            "s": "2017-02-14"
        },
        "merchId": {
            "s": "THIRDPARTYNEW"
        }
    },
    {
...
...
]


df = pd.read_json('combinedfiles/data.json', orient='records')
df.head()

Here is my output. 这是我的输出。

df DF

load the data properly, here dicts and list are showin inside the pandas dataframe. 正确加载数据,这里的dicts和list是在pandas数据帧中显示的。

I have tried other solutions here, but I believe they dont work. 我在这里尝试了其他解决方案,但我相信它们不起作用。

Here you go: 干得好:

import json
import pandas as pd

with open('test.json') as f:
    org = json.load(f)

transformed_dict = [{k:list(v.values())[0] for k,v in original_dict.items()} for 
original_dict  in org]
df = pd.DataFrame.from_records(transformed_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM