-1

I want to sort the data based on the 'Date' column but with some extra conditions. The data is given as below:

Type Date        col1    
A    02-01-2021  s      
A    03-01-2021  p   
A    01-01-2021  a    
B    03-01-2021  d
B    01-01-2021  f
B    02-01-2021  t
C    03-01-2021  w
C    02-01-2021  x
C    01-01-2021  m

When I apply sort_values() function on 'Date', I get the output values sorted, but not according to the type. The output I obtained is:

Type Date        col1    
A    01-01-2021  s      
A    01-01-2021  p   
A    01-01-2021  a    
B    02-01-2021  d
B    02-01-2021  f
B    02-01-2021  t
C    03-01-2021  w
C    03-01-2021  x
C    03-01-2021  m

But, I want the entire data sorted on the basis of the 'Date' column, and also ensure that the data is sorted according to other columns too.

Expected Output:

Type Date        col1    
A    01-01-2021  a      
A    02-01-2021  s   
A    03-01-2021  p    
B    01-01-2021  f
B    02-01-2021  t
B    03-01-2021  d
C    01-01-2021  m
C    02-01-2021  x
C    03-01-2021  w
Shubham
  • 23
  • 2

2 Answers2

0

The solution to sort multiple columns is


df.sort_values(by=['Date', 'col1'])

maziyank
  • 581
  • 2
  • 10
0

You have to pass in a list to sort_values()

df.sort_values(by=[‘col1’, ‘col2’])
Tyler Gallenbeck
  • 538
  • 3
  • 10