Vash
Vash

Reputation: 190

How do I update existing values and append new values to a dictionary?

I have N dataframes, from each of which I'm extracting df['col'].value_counts() and converting these to a dictionary so I have: my_dict = {'key1' : val1, 'key2' : val2, ... , 'keyM' : valM}

How do I update my_dict so that: If a random new dataframe D has the same key as a previous dataframe (e.g. 'key1'), then it adds the value to val1. In other words, if 'key1' had a value of 21 and the new dataframe has a value of 18 for the same key ('key1') , the dictionary key value should now be 'key1' : 39.

If however, the key does not exist, then it should create a new key with the relevant value.

Does that make sense? I feel like I'm overcomplicating this...

Upvotes: 1

Views: 65

Answers (3)

James
James

Reputation: 36608

Iterate over the key/values of new keys and update my_dict. You should also look into using defaultdict from the collections module

my_dict = {'key1': 21, 'key2': 10}
my_dict2 = {'key1': 18, 'key3': 5}

for k, v in my_dict2.items():
    if k in my_dict:
        my_dict[k] += v
    else:
        my_dict[k] = v

Using defaultdict

from collections import defaultdict

my_dict = defaultdict(int, {'key1': 21, 'key2': 10})
my_dict2 = {'key1': 18, 'key3': 5}

for k, v in my_dict2.items():
    my_dict[k] += v

Upvotes: 1

Thomas
Thomas

Reputation: 721

Here's another answer that uses collections as well:

from collections import defaultdict as ddict

some_list_of_dicts = [
    {'val1': 5, 'val2': 3},
    {'val1': 2, 'val2': 1, 'val3': 9},
]

my_dict = ddict(int)

for i in some_list_of_dicts:
    for key, count in i.items():
        my_dict[key] += count

print(dict(my_dict))

A defaultdict of int will be initialised to 0 when an unknown key is introduced.

Upvotes: 0

tzaman
tzaman

Reputation: 47780

collections.Counter is built for this.

from collections import Counter
c1 = Counter(my_dict)
c2 = Counter(my_other_dict)
c_sum = c1 + c2

On the other hand, you should be able to do this within pandas too; value_counts() returns a Series which you should be able to add to other Series objects directly and have it behave how you expect.

Upvotes: 4

Related Questions