简体   繁体   English

将多个嵌套的 JSON 文件读入 Pandas DataFrame

[英]Reading multiple nested JSON files into Pandas DataFrame

I have a problem writing the code that will read multiple json files from a folder in Python.我在编写将从 Python 中的文件夹中读取多个 json 文件的代码时遇到问题。

My json file example (file name: 20191111.json ) is like this:我的 json 文件示例(文件名:20191111.json)是这样的:

[
  {
    "info1": {
      "name": "John",
      "age" : "50"
      "country": "USA",
    },
    "info2": {
      "id1": "129",
      "id2": "151",
      "id3": "196",
    },
    "region": [
      {
        "id": "36",
        "name": "Spook",
        "spot": "2"
      },
      {
        "id": "11",
        "name": "Ghoul",
        "spot": "6"
      },
      {
        "id": "95",
        "lat": "Devil",
        "spot": "4"
      }
    ]
  }
  {
    "info1": {
      "name": "Mark",
      "age" : "33"
      "country": "Brasil",
    },
    "info2": {
      "id1": "612",
      "id2": "221",
      "id3": "850",
    },
    "region": [
      {
        "id": "68",
        "name": "Ghost",
        "spot": "7"
      },
      {
        "id": "75",
        "name": "Spectrum",
        "spot": "2"
      },
      {
        "id": "53",
        "name": "Phantom",
        "spot": "2"
      }
    ]
  }
]

My code:我的代码:

path = 'my_files_directory'
json_files = [pos_json for pos_json in os.listdir(path) if pos_json.endswith('.json')]

df = pd.DataFrame()

for file_ in json_files:
    file_df = pd.read_json(file_ )
    file_df['date'] = file_
    df = df.append(file_df)
    df = df.reset_index(drop=True) 

Output: Output:

             info1                    info2                   region                    date   
0 {'name': 'John', ...}    {'id1': '129', ...}    [{'id':'36','name':'Spook'...     20191111.json
1 {'name': 'Mark', ...}    {'id1': '61', ...}     [{'id':'36','name':'Ghost'...     20191111.json

Now I delete the first and second column because there is information that I don't need.现在我删除第一列和第二列,因为有我不需要的信息。 Then I want to extract 'name' information from 'region' column然后我想从“区域”列中提取“名称”信息

My code is:我的代码是:

df = df.drop(df.columns[[0,1]], axis=1)
df['name'] = [x[0]['name'] for x in df['region']]

Output: Output:

     name           date   
0    Spook     20191111.json
1    Ghost     20191111.json

But I'd like the corresponding DataFrame to look like this:但我希望相应的 DataFrame 看起来像这样:

      name           date  
0    Spook      20191111.json  
1    Ghoul      20191111.json  
2    Devil      20191111.json  
3    Ghost      20191111.json  
4    Spectrum   20191111.json  
5    Phantom    20191111.json  

What I have to do to get it?我必须做什么才能得到它? Thank you for your help.谢谢您的帮助。

This code is affecting your outcome because your dataframe only has two rows:此代码会影响您的结果,因为您的 dataframe 只有两行:

df['name'] = [x[0]['name'] for x in df['region']]

I changed it to:我将其更改为:

filename = '20191111.json'
df1=pd.read_json(filename)
df1 = df1.drop(columns=['info1', 'info2'])  

df2 = pd.DataFrame(columns=['name', 'date'])
names=[]
dates=[]
for x in df1['region']: 
   for name in x: 
     names.append(name['name']) 
     dates.append(filename)
df2['name']=names
df2['date']=dates 

and i get the correct data.我得到了正确的数据。 you list comprehension can't add more rows than you already are in the data frame, so i created a new one.您列出的理解不能添加比您已经在数据框中更多的行,所以我创建了一个新行。

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

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