Home > Mobile >  Pandas datfaframe, how to order a column grouped by 2 other columns
Pandas datfaframe, how to order a column grouped by 2 other columns

Time:10-05

I have a dataframe like this one, with many more rows:

zone keyword sales
nyc1 iphone 10
nyc1 smart tv 6
nyc1 iphone 12
nyc2 laptop 22
slc1 iphone 3
slc2 radio 5
la1 iphone 10
la1 tablet 22
la1 tablet 5

How can I get another dataframe where for each zone/keyword I get the sum of the sales column (grouped by zone/keyword) in descending order? For this example it should look like this (I don't want to reorder based on the other 2 columns, only sales):

zone keyword sales
nyc1 iphone 22
nyc1 smart tv 6
nyc2 laptop 22
slc1 iphone 3
slc2 radio 5
la1 tablet 27
la1 iphone 10

I already grouped the columns using

df_sales = df_sales.groupby(['zone','keyword'])['sales'].sum()

But the result is a series with the sum-of-sales column not in order.

Using reset_index and sort_values does order by sales, but removes the groupby and order the whole dataframe...

.reset_index().sort_values('sales', ascending=False)

How can I get a dataframe like the one above?

CodePudding user response:

After you complete your groupby, you can use sort_values

df_sales = df_sales.groupby(['zone','keyword'])['sales'].sum()
sorted_df = df_sales.sort_values(by=['zone'])

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html

CodePudding user response:

df_sales.groupby(['zone','keyword'])['sales'].sum().reset_index().sort_values('sales', ascending=False)

reset_index reverts series back to dataframe and after that you can sort values.

  • Related