I have a MultiIndex series look like this:
user_id cookie browser
1 1_1 [chrome45]
2 2_1 [IE 7]
2 2_2 [IE 7, IE 8]
There are two levels to this MultiIndex, user_id
and cookie
. The value is the browser.
What I want to do is to count the number of times a user uses a different browser.
So for user 1 in this case, he only used 1 browser. But for user 2, he used three browsers (IE7 appeared twice under different cookies, so I count it twice instead of once)
How can I loop through it and get a result like this:
r = defaultdict(int)
for user_id in multiIndex_series:
for cookie in multiIndex_series[user_id]:
r[user_id] += len(multiIndex_series[user_id][cookie]) # I don't know how to get user_id out of the MultiIndex series