Issue
Starting with a .csv with a format roughly like
date, qualitative info, value
I've tried to plot this value in pyplot but run into the issue where I have multiple values for the same date since I don't care about the qualitative info in the .csv.
Is there a way to either add the two values together during the plotting process or to merge the lists of dicts in such a way where I can go from:
[{'interval_start_timestamp': '2021-04-13', 'leavers': 3}, {'interval_start_timestamp': '2021-04-13', 'leavers': 16}, {'interval_start_timestamp': '2021-04-14', 'leavers': 6}, {'interval_start_timestamp': '2021-04-14', 'leavers': 10}]
to:
[{'interval_start_timestamp': '2021-04-13', 'leavers': 19}, {'interval_start_timestamp': '2021-04-14', 'leavers': 16}]
keeping in mind that there are instances where a date is not repeated, is there a way to write it universally so that a new .csv in the same format would behave as desired?
Solution
Your data is in a bit of an awkward format, so I think it is best to format it to something easier to use and format it back later:
# loop over all dictionaries in the input list and reformat them
merged_dict = {} # use timestamps as keys, leavers as values
for dictionary in csv_dicts:
timestamp = dictionary["interval_start_timestamp"]
leavers = dictionary["leavers"]
# check if a key already exists
if dictionary["interval_start_timestamp"] in merged_dict:
merged_dict[timestamp] += leavers
else:
merged_dict[timestamp] = leavers
# use the data for whatever you want
plt.plot(merged_dict.keys(), merged_dict.values())
# format back to the old format using list comprehension
reformatted_dict = [{"interval_start_timestamp": timestamp, "leavers": leavers}
for timestamp, leavers in merged_dict.items()]
This outputs a plot and the merged data:
>>> reformatted_dict
[{'interval_start_timestamp': '2021-04-13', 'leavers': 19}, {'interval_start_timestamp': '2021-04-14', 'leavers': 16}]
Answered By - asdf101
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.