I have a df like this:
| parent | child |
|---|---|
| Susie | jose |
| Susie | bob |
| Susie | bob |
| Susie | frank |
| Allen | bob |
| Allen | frank |
and I want it to look like this:
| Parent | child_jose | child_bob | child_frank |
|---|---|---|---|
| Susie | 1 | 2 | 1 |
| Allen | 0 | 1 | 1 |
which is to just take the count of each child the parent has and make a column for that child with the count of how many kids each parent has
I used this code to first group by parent name:
''' by_parent = df.groupby(["ParentName", "ChildName"])['ChildName'].count().to_frame() '''
Which looks right when I get the output, except that the count column is named 'ChildName'.
So after this code it looks like
| ParentName | ChildName | ChildName |
|---|---|---|
| Susie | jose | 1 |
| bob | 2 | |
| frank | 1 | |
| Allen | bob | 1 |
| frank | 1 | |
| jose | 0 |
I have tried a few of the similar solutions on here but am getting no luck with getting the column names straight on this. After using to_frame() it looks right but when I call df.columns, only 'ChildName' is shown. Any help with how to get this first step down and then possibly help with pivoting off of the childName column would be super helpful. Thanks in advance and please comment if you need additional clarification.