熊猫逆json_normalize

时间:2019-02-20 00:19:27

标签: json python-3.x pandas normalize

我刚刚发现json_normalize函数,该函数在获取JSON对象并给我一个pandas Dataframe时效果很好。现在,我想要反向操作,该操作采用相同的Dataframe并给我一个与原始json具有相同结构的json(或类似json的字典,我可以轻松地将其转换为json)。

下面是一个示例:https://hackersandslackers.com/json-into-pandas-dataframes/

他们采用JSON对象(或类似JSON的python字典)并将其转换为数据框,但是我现在想采用该数据框并将其转换回类似JSON的字典(以便以后转储至json文件)。

5 个答案:

答案 0 :(得分:2)

我通过几个函数实现了

def set_for_keys(my_dict, key_arr, val):
    """
    Set val at path in my_dict defined by the string (or serializable object) array key_arr
    """
    current = my_dict
    for i in range(len(key_arr)):
        key = key_arr[i]
        if key not in current:
            if i==len(key_arr)-1:
                current[key] = val
            else:
                current[key] = {}
        else:
            if type(current[key]) is not dict:
                print("Given dictionary is not compatible with key structure requested")
                raise ValueError("Dictionary key already occupied")

        current = current[key]

    return my_dict

def to_formatted_json(df, sep="."):
    result = []
    for _, row in df.iterrows():
        parsed_row = {}
        for idx, val in row.iteritems():
            keys = idx.split(sep)
            parsed_row = set_for_keys(parsed_row, keys, val)

        result.append(parsed_row)
    return result


#Where df was parsed from json-dict using json_normalize
to_formatted_json(df, sep=".")

答案 1 :(得分:1)

df.to_json(path)

df.to_dict()

答案 2 :(得分:0)

让我扔两分钱

向后转换后,您可能需要从生成的json中删除空列 因此,我检查了val!= np.nan。但是您不能直接执行此操作,而是需要检查val == val与否,因为np.nan!=本身。 我的版本:

def to_formatted_json(df, sep="."):
result = []
for _, row in df.iterrows():
    parsed_row = {}
    for idx, val in row.iteritems():
        if val == val:
            keys = idx.split(sep)
            parsed_row = set_for_keys(parsed_row, keys, val)

    result.append(parsed_row)
return result

答案 3 :(得分:0)

更简单的方法:
仅使用1个功能...

def df_to_formatted_json(df, sep="."):
    """
    The opposite of json_normalize
    """
    result = []
    for idx, row in df.iterrows():
        parsed_row = {}
        for col_label,v in row.items():
            keys = col_label.split(".")

            current = parsed_row
            for i, k in enumerate(keys):
                if i==len(keys)-1:
                    current[k] = v
                else:
                    if k not in current.keys():
                        current[k] = {}
                    current = current[k]
        # save
        result.append(parsed_row)
    return result

答案 4 :(得分:0)

这对我来说似乎是一种解决方案。它设计为可在一行中的数据帧上工作,但可以轻松地在大型数据帧上循环。

class JsonRecreate():
    
    def __init__(self, df):
        self.df = df

    def pandas_to_json(self):
        df = self.df
        # determine the number of nesting levels
        number_levels = np.max([len(i.split('.')) for i in df.columns])
        # put all the nesting levels in an a list
        levels = []
        for level_idx in np.arange(number_levels):
            levels.append(np.array([i.split('.')[level_idx] if len(i.split('.')) > level_idx else ''
                                    for i in df.columns.tolist()]))
        self.levels = levels
        return self.create_dict(upper_bound = self.levels[0].shape[0])

    def create_dict(self, level_idx = 0, lower_bound = 0, upper_bound = 100):
        ''' Function to create the dictionary starting from a pandas dataframe generated by json_normalize '''
        levels = self.levels
        dict_ = {}
        # current nesting level
        level = levels[level_idx]
        # loop over all the relevant elements of the level (relevant w.r.t. its parent)
        for key in [i for i in np.unique(level[lower_bound: upper_bound]) if i != '']:
            # find where a particular key occurs in the level
            correspondence = np.where(level[lower_bound: upper_bound] == key)[0] + lower_bound
            # check if the value(s) corresponding to the key appears once (multiple times)
            if correspondence.shape[0] == 1:
                # if the occurence is unique, append the value to the dictionary
                dict_[key] = self.df.values[0][correspondence[0]]
            else:
                # otherwhise, redefine the relevant bounds and call the function recursively
                lower_bound_, upper_bound_ = correspondence.min(), correspondence.max() + 1
                dict_[key] = self.create_dict(level_idx + 1, lower_bound_, upper_bound_)
        return dict_

我用一个简单的数据帧进行了测试,例如:

df = pd.DataFrame({'a.b': [1], 'a.c.d': [2], 'a.c.e': [3], 'a.z.h1': [-1], 'a.z.h2': [-2], 'f': [4], 'g.h': [5], 'g.i.l': [6], 'g.i.m': [7], 'g.z.h1': [-3], 'g.z.h2': [-4]})

json中的顺序未完全保留在生成的json中,但是如果需要,可以轻松处理。

相关问题