9951 explained code solutions for 126 technologies


python-pandasHow to sort dataframe by multiple columns


import pandas as pd

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

df = df.sort_values(by=['price', 'bonus'])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', 'bonus'

columns to sort by