Geek Logbook

Tech sea log book

How to Aggregate Values by Date and Sum Them in Python

Problem Statement

Suppose you have a list of transactions or events, each associated with a date and a numeric value (e.g., sales amount, transaction amount). Your goal is to aggregate these values by date and calculate the total sum for each date.

Solution Approach

We’ll use Python’s dictionary data structure to achieve this. Dictionaries allow us to store key-value pairs, where each key uniquely identifies a date, and the corresponding value is the sum of all numeric values associated with that date.

Step-by-Step Implementation

Let’s go through the implementation step by step:

  1. Initialize an Empty Dictionary: Start with an empty dictionary final_dict where we will store our aggregated results.
  2. Iterate Over Each Item: Loop through each transaction or event represented as a dictionary in the transactions list.
  3. Extract Date and Value: For each item, extract the date (purchase_date) and the numeric value (price) from the dictionary.
  4. Aggregate Values: Check if the date_string (formatted date) already exists as a key in final_dict. If it does, add the current price to the existing value. If it doesn’t, initialize the key with the current price.
  5. Return the Final Dictionary: After iterating through all items, final_dict will contain aggregated sums for each unique date.
def aggregate_prices(transactions):
    final_dict = {}
    for item in transactions:
        date_string = ''
        price = 0
        for k, v in item.items():
            if k == 'price':
                price = v
            if k == 'purchase_date':
                date_string = v.split('-',1)[-1]
        
        if date_string in final_dict:
            final_dict[date_string] += price
        else:
            final_dict[date_string] = price 
    
    return final_dict

transactions = [
    {'purchase_date': '2024-05-22', 'price': 12000},
    {'purchase_date': '2024-05-22', 'price': 2400},
    {'purchase_date': '2024-06-22', 'price': 3000},
]

result = aggregate_prices(transactions)
print(result)

Result

{'05-22': 14400, '06-22': 3000}
Tags: