I have a pandas dataframe:
df = pd.DataFrame(
{
"id": ["K0", "K0", "K0", "K1", "K1", "K2", "K2","K2"],
"name": ["Peter", "Peter", "Max", "Jim", "Lucy", "Lucy", "Lucy", "Pam"]
}
)
I want to get the frequency of each name for each ID. First I tried to groupby ID
. Next I tried to count the number of unique names and write the result in a new column. Did't realy work in my case. The final df should look like this: