1

I have a big dataframe with two columns A and B:

        A              B                                         
0     US1           URL1                                                                                              
1     US2           URL1                                                                                              
2     US1           URL2
3     US3           URL3
4     US2           URL3
4     US2           URL3

I'd like to obtain a dataframe in which for each unique element in B the count of the unique elements in B and the number of unique element in A associated with b:

         B            Imp      Aud                                       
0     URL1              2        2                                                                                            
1     URL2              1        1                                                                                          
2     URL3              3        2

1 Answer 1

1

You can use groupby.agg; with size to get the number of elements and nunique to get the number of unique elements:

df.groupby('B', as_index=False).A.agg({'Imp': 'size', 'Aud': 'nunique'})

#      B    Imp   Aud
#0  URL1      2     2
#1  URL2      1     1
#2  URL3      3     2
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Psidom !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.