jquery - flatten a deep json structure with jq for pandas -
i trying flatten json file pandas dataframe , found solution here. in case json has many different attributes seems tedious spell out rule every field manually. isn't possible flatten every attribute in json file automatically?
i made solve similar problem. might not work in case, maybe take similar approach.
def nested_dataframe(d):     assert type(d) dict  # may nested dict     types = map(type, d.values())     if dict not in types:         # 1 un-nested dict. make dataframe.         return dataframe.from_dict({k: list([v]) k, v in d.items()}, orient='index')     if all([t dict t in types]):         # dict of dicts.         # call nested_dataframe on each item, , concatenate results.         return pd.concat([nested_dataframe(a) in d.values()], keys=d.keys())     else:         raise valueerror("this doesn't work on dicts unequal depths.") 
Comments
Post a Comment