熊猫逆json_normalize

熊猫逆json_normalize,第1张

熊猫逆json_normalize

我用几个功能实现了它

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_dictdef 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_normalizeto_formatted_json(df, sep=".")


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4953061.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存