Skip to content Skip to sidebar Skip to footer

Sort Df Based On Multiple Conditions

I have a df as shown below df: product_x year total_price total_sale A 2016 50 200 B 2016 200 100

Solution 1:

That should work fine. If you want sort order to be different by columns, you can pass a list to ascending=:

df = df.sort_values(['year_c', 
                'total_sale'], ascending=[True,False])

See docs for more details: pd.DataFrame.sort_values()


Solution 2:

Define the sort order separately for each column:

df.sort_values(['year_c', 'total_sale'], ascending=[True, False])

(ascending by year_c, descending by total_sale).


Post a Comment for "Sort Df Based On Multiple Conditions"