【问题标题】:Nested json containing list of json-strings to dataframe包含 json 字符串列表到数据框的嵌套 json
【发布时间】:2021-08-07 15:26:44
【问题描述】:

我想将以下json 字符串放入数据框:

jsonstr = {
  "id": "12345",
  "ename": "A4.txt",
  "Zoom1": {
    "Zoom1_res": [
      {
        "code": "A1",
        "x": 3211,
        "y": 677,
        "part": "11",
        "lace": "29",
        "name": "COVER"
      },
      {
        "code": "A4",
        "x": 3492,
        "y": 1109,
        "part": "10",
        "lace": "19",
        "name": "ARMOUR"
      }
    ]
  },
  "iSize": {
    "width": 4608,
    "height": 3456
  },
  "Action": {
    "AA": {
      "detect": [
        {
          "class": "aa",
          "prob": 0.92,
          "Box": {
            "x0": 4406,
            "y0": 670,
            "x1": 4558,
            "y1": 760
          }
        },
        {
          "class": "aa",
          "prob": 0.92,
          "Box": {
            "x0": 3762,
            "y0": 655,
            "x1": 3913,
            "y1": 747
          }
        }
      ]
    }
  }
}

以下列方式使用json_read

df =pd.read_json(jsonstr)

返回

 id   ename                                              Zoom1  \
Zoom1_res  12345  A4.txt  [{'code': 'A1', 'x': 3211, 'y': 677, 'part': '...   
width      12345  A4.txt                                                NaN   
height     12345  A4.txt                                                NaN   
AA         12345  A4.txt                                                NaN   

            iSize                                             Action  
Zoom1_res     NaN                                                NaN  
width      4608.0                                                NaN  
height     3456.0                                                NaN  
AA            NaN  {'detect': [{'class': 'aa', 'prob': 0.92, 'Box...  

pd.json_normalize(df['Action'])

返回错误

AttributeError: 'float' object has no attribute 'values'

所以,我认为申请

from ast import literal_eval

pd.json_normalize(df['Action'].apply(lambda x: literal_eval(x)["detect"]).explode())

可能会解决问题,但该列中有nan,所以即使这样也行不通。

我真正想要的是:

最好的世界:id, ename, code, x, y, x0, y0, x1, y1

所有其他数据对我来说毫无价值。

感谢任何见解!

【问题讨论】:

    标签: json python-3.x pandas dataframe


    【解决方案1】:

    看到您的 JSON 嵌套在多个级别上,

    1. 创建子数据框

    df1 = pd.json_normalize(jsonstr, record_path=['Action','AA','detect'],  meta=['id','ename'])
    df2 = pd.json_normalize(jsonstr, record_path=['Zoom1','Zoom1_res'],  meta=['id','ename'])
    

    2. 将数据转换为 NaN

    根据我的理解,gBOX 和 BOX 是同一个属性,所以你可以通过这种方式合并它们,你可以玩弄这些并获取所需的数据
    df3 = df1.apply(lambda x: pd.Series(x.dropna().values), axis=1)
    df3.columns = ['class','prob','x0','y0','x1','y1','id','ename']
    

    3.根据您的数据获取所需的列

    df4 = pd.merge(df3, df2, on=['id','ename'])
    df4 = df4.iloc[:,[6,7,8,9,10,2,3,4,5]]
    

    【讨论】:

    • 这真是太好了!不过有一条评论:, left_index=True, right_index=True 对我不起作用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2022-12-13
    • 2022-07-08
    相关资源
    最近更新 更多