【问题标题】:Is there a possibility to convert complex json to pandas dataframe?是否有可能将复杂的 json 转换为 pandas 数据框?
【发布时间】:2019-08-16 18:35:38
【问题描述】:

我知道到目前为止有非常相似的问题,但我只是没有正确的想法抱歉。

我想将复杂的 json 转换为干净的 pandas 数据框。

到目前为止我的代码:

with open('JSON_Input.json', 'r') as json_file:
    json_data = json.load(json_file)
    json_data = json.loads(json_data)

这将创建以下复杂的嵌套 json 对象:

json_data 

{'time': 0,
 'day1': [{'time': 0,
   'coordinates': [{'x': 1202.5, 'y': 486, 'time': 3276},
    {'x': 1162.5, 'y': 484, 'time': 3331},
    {'x': 742.5, 'y': 492.5, 'time': 3487},
    {'x': 673.5, 'y': 501.5, 'time': 3514},
    {'x': 636, 'y': 508.5, 'time': 3539}]},
  {'path': 'path1',
   'time': 3558,
   'coordinates': [{'x': 1237, 'y': 173, 'time': 5437},
    {'x': 1240, 'y': 182, 'time': 5601},
    {'x': 1260, 'y': 161, 'time': 7289},
    {'x': 1263, 'y': 165, 'time': 7465},
    {'x': 1482, 'y': 114.5, 'time': 8072},
    {'x': 1482, 'y': 114, 'time': 8197},
    {'x': 1482, 'y': 126.5, 'time': 9539}]},
  {'path': 'path2',
   'time': 23620,
   'coordinates': [{'x': 227.5, 'y': 420, 'time': 25228},
    {'x': 235, 'y': 418, 'time': 25426}]},
  {'path': 'path3',
   'time': 35891,
   'coordinates': [{'x': 681.5, 'y': 431, 'time': 36648},
    {'x': 704.5, 'y': 427.5, 'time': 36661},
    {'x': 874.5, 'y': 420.5, 'time': 36714},
    {'x': 909.5, 'y': 422, 'time': 36734}]}],
 'day2': {'path': 'path4',
  'time': 36743,
  'coordinates': [{'x': 600, 'y': 622.5, 'time': 37390},
   {'x': 603, 'y': 594.5, 'time': 37448},
   {'x': 605, 'y': 541.5, 'time': 37478},
   {'x': 608.5, 'y': 481.5, 'time': 37495},
   {'x': 620, 'y': 369, 'time': 37530},
   {'x': 624.5, 'y': 329, 'time': 37547},
   {'x': 636, 'y': 366, 'time': 38043}]}}

现在如何从这个 json 文件中得到一个干净的数据框?

【问题讨论】:

  • 请显示所需的数据帧输出
  • 这是下一个问题。我刚刚得到了这个 json 文件,并被告知要分析它并制作一个可以在 Excel 中显示的数据框。我和我的同事都不知道预期的数据帧输出 =/
  • 这很难提供帮助。因此,您既可以准确地提供需要分析的内容,也可以回到老板那里询问更多信息
  • 是的,我会这样做。谢谢你的时间。当我有更多的信息时,我会再写。
  • @Larsus123 你这样做的任何具体原因json_data = json.load(json_file) & json_data = json.loads(json_data)

标签: python json


【解决方案1】:

这很棘手。你最终会得到很多空值,而且我也不知道你希望结束 datframe 的样子。但也许这可以让你朝着正确的方向前进:

jsonObj = {'time': 0,
 'day1': [{'time': 0,
   'coordinates': [{'x': 1202.5, 'y': 486, 'time': 3276},
    {'x': 1162.5, 'y': 484, 'time': 3331},
    {'x': 742.5, 'y': 492.5, 'time': 3487},
    {'x': 673.5, 'y': 501.5, 'time': 3514},
    {'x': 636, 'y': 508.5, 'time': 3539}]},
  {'path': 'path1',
   'time': 3558,
   'coordinates': [{'x': 1237, 'y': 173, 'time': 5437},
    {'x': 1240, 'y': 182, 'time': 5601},
    {'x': 1260, 'y': 161, 'time': 7289},
    {'x': 1263, 'y': 165, 'time': 7465},
    {'x': 1482, 'y': 114.5, 'time': 8072},
    {'x': 1482, 'y': 114, 'time': 8197},
    {'x': 1482, 'y': 126.5, 'time': 9539}]},
  {'path': 'path2',
   'time': 23620,
   'coordinates': [{'x': 227.5, 'y': 420, 'time': 25228},
    {'x': 235, 'y': 418, 'time': 25426}]},
  {'path': 'path3',
   'time': 35891,
   'coordinates': [{'x': 681.5, 'y': 431, 'time': 36648},
    {'x': 704.5, 'y': 427.5, 'time': 36661},
    {'x': 874.5, 'y': 420.5, 'time': 36714},
    {'x': 909.5, 'y': 422, 'time': 36734}]}],
 'day2': {'path': 'path4',
  'time': 36743,
  'coordinates': [{'x': 600, 'y': 622.5, 'time': 37390},
   {'x': 603, 'y': 594.5, 'time': 37448},
   {'x': 605, 'y': 541.5, 'time': 37478},
   {'x': 608.5, 'y': 481.5, 'time': 37495},
   {'x': 620, 'y': 369, 'time': 37530},
   {'x': 624.5, 'y': 329, 'time': 37547},
   {'x': 636, 'y': 366, 'time': 38043}]}}










import pandas as pd
import re


def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x

    flatten(y)
    return out

results = pd.DataFrame()
for k in jsonObj:

    flat = flatten_json(jsonObj[k])                      


    temp_df = pd.DataFrame()
    special_cols = []

    columns_list = list(flat.keys())
    for item in columns_list:
        try:
            row_idx = re.findall(r'\_(\d+)\_', item )[0]
        except:
            special_cols.append(item)
            continue
        column = re.findall(r'\_\d+\_(.*)', item )[0]
        column = column.replace('_', '')

        row_idx = int(row_idx)
        value = flat[item]

        temp_df.loc[row_idx, column] = value

    for item in special_cols:
        temp_df[item] = flat[item]

    if 'day' in k:
        temp_df['day'] = k
    results = results.append(temp_df).reset_index(drop=True)

results = results.dropna(axis=1, how='all')

【讨论】:

  • 感谢您的支持。我找到了一种方法程序并获得了一些见解。如果这个问题扩大,我会试试你的代码,它看起来比我的效率高得多。再次感谢!
猜你喜欢
  • 2019-05-14
  • 2019-07-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2023-03-11
  • 1970-01-01
  • 2020-10-31
相关资源
最近更新 更多