-1

I'm trying to sort two pandas dataframe columns. I understand that Python has its own built-in function:

.sort()

But I'm wondering if Pandas has this function too and if it can be done with two columns together, as a pair.

Say for example I have the following dataset:

   sum         feature        
0  5.1269        3
1  2.8481        2     
2  -1.472        1  
3  -3.212        0

I want to obtain this:

   sum         feature        
0  -3.212        0
1  -1.472        1     
2  2.8481        2  
3  5.1269        3

Basically what I am doing here, is I am sorting the column 'feature' to get it from minimum to maximum, however I want the corresponding values in 'sum' to also change.

Can someone please help me out with this? I have seen other posts around Stackoverflow on this, however I have not found a detailed answer explaining the process, or an answer for this specific question.

saurish
  • 68
  • 1
  • 11

1 Answers1

2

Just use:

df.sort_values('feature')

For resetting index:

df=df.sort_values('feature').reset_index(drop=True)
print(df)

      sum  feature
0 -3.2120        0
1 -1.4720        1
2  2.8481        2
3  5.1269        3
anky
  • 74,114
  • 11
  • 41
  • 70