Can't aggregate with function using .groupby

Hi all,

I merged various datasets together. The output is the following dataframe:

product_town = pd.merge(left=product_train, right=town_train, how='outer', on='Producto_ID')
product_town.sort_values(by='count_name', ascending=False).head(50)

I grouped the dataframe using the following code and got:

product_town.groupby(['name','count_name'])[['name','count_name','State', 'location']].head(50).dropna().sort_values(by='count_name', ascending=False)


My desired output is the unique values, for example
Pan Blanco 39.0 Mexico, D.F
Tuinky Fresas 2.0 México, D.F
Pan Blanco 39.0 Aguascalientes

I believe I am missing an aggregate function but I can’t get it to work. Please help.

1 Like


What is your motivation for grouping with count name. When you use groupby, you can use sum to add all the items in a particular group. Here you will need to do so with the count_name. Otherwise, you can use size to just calculate the total number of rows in a group. If you want to use more than one aggregate function, you have to use the .agg().

Let me asssume this is what you want to do: take the columns [name, count_name, State, location] and perform some operations on them.

product_down[['name', 'count_name', 'State', 'location']].groupby(['name', 'State']).agg({'count_name': ['sum', 'size']})