9951 explained code solutions for 126 technologies


python-pandasHow to sort dataframe in descending order


import pandas as pd

df = pd.DataFrame({
  'phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'price': [204, 704, 404, 405, 305]
})

df = df.sort_values(by=['price'], ascending=False)ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

sort_values

sorts dataframe by column values

by=

column names to sort by

'price'

we're sorting by price column

ascending=False

to sort in descending order