How to efficiently create a dict with key/value, where the value is the number of occurrences for a given key?
I'm currently doing like this:
dict_map = dict()
for car in data_frame["cars"]:
if car in dict_map :
dict_map.update({car : dict_counter.get(car) 1})
else:
dict_map.update({car : 1})
return dict_map
Is there any other way to do it in a more efficient way or using less code?
CodePudding user response:
This is actually plenty efficient, just unidiomatic. Don't use .update
here, and there's no need for the if-else.
dict_map = {}
for car in data_frame['cars']:
dict_map[car] = dict_map.get(car, 0) 1
But this is such a common use-case, the standard library includes collections.Counter
which is just a dict subclass specialized for this very thing, and you can get this using
import collections
dict_map = collections.Counter(data_frame["cars"])
However since you are using pandas, you should use the built-in pandas API first.
>>> data_frame = pd.DataFrame(dict(cars=['a','b','c','a','a','c']))
>>> data_frame['cars'].value_counts()
a 3
c 2
b 1
Name: cars, dtype: int64
>>> data_frame['cars'].value_counts().to_dict()
{'a': 3, 'c': 2, 'b': 1}
CodePudding user response:
from collections import Counter
dict_map = dict(Counter(data_frame["cars"])