I would like to combine records that have an identical id number. For example, suppose I have this DataFrame:
df=pd.DataFrame({'id': [1, 2, 2, 4], 'animal': ['dog', 'cat', 'bear', 'mouse']})
# just rearranging the order a bit
df=df[['id', 'animal', 'name']]
id animal name
1 dog john
2 cat mary
2 bear mary
4 mouse joe
What I'd like to end up with is a way to get the following:
id name animal more_animals
1 john dog NaN
2 mary cat bear
4 joe mouse NaN
I could use df[df.duplicated('id', 'last')]
to find the duplicated rows, then loop through each duplicated id and add details to the new column but wondered if there was something a bit more elegant.
Anything come to mind?