How To Pack Multilines Results (ordereddict With Differents Keys) Into Pandas Dataframe?
I did a loop over multiple files to get as a result an OrderedDict for each one (possibility to have different keys, which means not the same from an OrderedDict to another). I wan
Solution 1:
following your the 3 examples of OrderedDicts you provided Note that the first OrderedDict you provided has a single element tuple ('nom', ) which I changed to ('nom',' ').
following 4 steps to achieve desired result:
list_of_ordered_dicts = [od1,od2,od3]
# flat all items for dicts together in a list
all_items =[item for dict_items in list_of_ordered_dicts
for item in dict_items.items() ]
# create a setwithall columns
all_columns =set(dict(all_items).keys())
# update ordered dicts withnew columns, setting missing columns valuestoNonefor ordered_dict in list_of_ordered_dicts:
missing_columns = all_columns -set(ordered_dict.keys())
forcolumnin missing_columns:
ordered_dict.setdefault(column, None)
# create dataframe
df = pd.DataFrame(list_of_ordered_dicts, columns=all_columns)
Post a Comment for "How To Pack Multilines Results (ordereddict With Differents Keys) Into Pandas Dataframe?"