I have a list of dicts as shown below. I wish to write the dicts to multiple excel or csv files depending on the keys. if the keys are the same they should be in one file.
my_list_of_dicts:
[{'john': ['0', '100']}, {'john': ['4', '101']}, {'john': ['0', '102']}, {'mary': ['2', '100']}, {'mary': ['5', '101']}, {'mary': ['4', '102']}, {'mary': ['1', '103']}, {'sam': ['4', '100']}, {'sam': ['3', '101']}, {'sam': ['12', '102']}, {'paul': ['2', '100']}, {'hay': ['2', '100']}, {'hay': ['1', '102']}, {'mercy': ['4', '101']}]
My code so far:
x = []
i = 0
for ii, line in enumerate(my_list_of_dicts):
with open("out_%s.csv" % i, 'w+') as f:
if line.keys() not in x:
x.append(line.keys())
i += 1
pd.DataFrame.from_dict(data=line, orient='index').to_csv(f, header=False)
else:
pd.DataFrame.from_dict(data=line, orient='index').to_csv(f, header=False)
Result:
I am getting the desired number of files but not the content.
Expectation:
I expect to get files corresponding to each key i.e (john, mary, sam, jay, paul, hay, and mercy) with the below content. Using john as example:
john, 0, 100
john, 4, 101
john, 0, 102
I am not sure how to proceed or if I even need enumerate. Thank you
A better idea is to aggregate your data into a single dataframe and then iterate a groupby object:
# construct dataframe from list of dictionaries
df = pd.DataFrame([[k, *v] for dct in L for k, v in dct.items()])
df[[1, 2]] = df[[1, 2]].apply(pd.to_numeric)
# iterate groupby object and export to separate CSV files
for key, df_key in df.groupby(0):
df_key.to_csv(f'{key}.csv', index=False, header=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With