Apolo Reis
Apolo Reis

Reputation: 97

I want to group my list of lists by date and sum the values where the dates match

I have a list of lists like that:

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

I want to group it by date and sum the values where the dates are equal like that:

[['05-05-2015', 70], ['07-05-2015', -225]]

I found this solution: python sum up list elements in list of lists based on date as unique

It seems to solve my problem. But I found the code very complex to understand and replicate in my code.

Could someone explain me how I can do this?

Upvotes: 1

Views: 292

Answers (3)

try grouped by and sum

lst = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

df=pd.DataFrame(columns=['Date','Amount'])
for item in lst:
    print(item)
    df=df.append({'Date':item[0],'Amount':item[1]},ignore_index=True)
    
df.set_index('Date',inplace=True)
grouped=df.groupby(df.index)['Amount'].sum()

res=[[item[0],item[1]] for item in grouped.items()]

print(res)

output:

[['05-05-2015', 70], ['07-05-2015', -225]]

Upvotes: 0

Nic Laforge
Nic Laforge

Reputation: 1876

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

d = {}

for date, value in l:
    d[date] = d.get(date, 0) + value

print(list(d.items()))

Output:

[('05-05-2015', 70), ('07-05-2015', -225)]

Upvotes: 2

Hihikomori
Hihikomori

Reputation: 990

This code is easier for understanding:

l = [['05-05-2015', -40], ['05-05-2015', 30], ['07-05-2015', -75], ['05-05-2015', -40], ['05-05-2015', 120], ['07-05-2015', -150]]

result = {}

for entry in l:
    #  if date is not in dictionary, adding it to dictionary, other way making summation 
    if entry[0] not in result:
        result[entry[0]]=entry[1]
    else:
        result[entry[0]]+=entry[1]
print(result)

Upvotes: 1

Related Questions