How to groupby and calculate mode inline?

# # Import Libs
import pandas as pd

# # Data
# Create DataFrame
source = pd.DataFrame({
    'CPF'  : ['0000','0000', '0000', '1111','1111'], 
    'Cat'  : ['AAA', 'BBB','CCC', 'DDD', 'AAA'],
    'Qtde' : [3,3,3,12,1]})


# I want to add all the categories whose quantity is identical
forma1 = source.groupby(['CPF', 'Qtde'])['Cat'].apply(','.join).reset_index()


# I want the result of the biggest category
forma2 = forma1.sort_values('Qtde', ascending=False).drop_duplicates('CPF').sort_index().reset_index(drop=True)

Leave a Reply