I have a dataframe column that contains python lists of tags. I need to create a dictionary that counts how many times a tag was used. I did it this way:
tags_use_count = {}
def count_tags(tag_list):
for tag in tag_list:
if tag in tags_use_count:
tags_use_count[tag] += 1
else:
tags_use_count[tag] = 1
q2019['Tags'].apply(count_tags)
It works just fine, but I wonder if this is a good way of doing it. Somehow, using apply that way seems like a crappy workaround that seasoned coders would frown upon. (It's not what apply was built for, I guess.) The dataset is small, so I guess I could use iterrows to loop through the column, but I understand it's not a good idea for larger datasets and I wonder if my approach would be the go-to in that case or if there's a a better way.