I have some data like this:
+---------+----------+----------+
| groupX | groupY | id |
+---------+----------+----------+
| A | 1 | e13r2 |
| B | 2 | efwu1 |
| A | 1 | efgi3 |
| B | 4 | eoij9 |
+---------+----------+----------+
df = pd.DataFrame([['A',1,'e13r2'],['B',2,'efwu1'],['A',1,'efgi3'],['B',4,'eoij9']],
columns=['groupX','groupY','id'])
Allow me to demonstrate what I am aiming for:
I want to group by groupX, and then group by groupY. So for instance, this would give us everything in group A. Group A is further divided into subgroups. In this case, there is only subgroup 1 within group A. Within this subgroup are two ids: e13r2 and efgi3. Similarly, within group B are the subgroups 2 and 4. Subgroup 2 contains id efwu1 and subgroup 4 contains id eoij9.
So group A contains one subgroup. This subgroup has two ids within it. On average, the subgroups within group A have 2 ids within them.
Group B contains two subgroups. Subgroup 2 contains one id and subgroup 4 contains one id. On average, the subgroups within group B have 1 id within them.
So I am taking the mean number of counts within the subgroups of each group.
I am looking for a Pandas command that would return this:
+---------+--------------------------------+
| groupX | mean counts within subgroups |
+---------+--------------------------------+
| A | 2 |
| B | 1 |
+---------+--------------------------------+
My attempt so far is this:
df.groupby(['groupX','groupY']).count()
But I cannot see how to proceed from here.