0

I am trying to sort a dataframe, but the sort_values method seems not to be working. I have checked other stackoverflow posts on the same issue, but they do not seem to help me.

Code.py

    var_df = pd.DataFrame(columns=['Variable','Remaining values','Degrees'])
    var_df.loc[0] = ['V1', 1, 2]
    var_df.loc[1] = ['V2', 2, 3]
    var_df.loc[2] = ['V3', 2, 1]
    var_df.loc[3] = ['V4', 4, 5]
    var_df.loc[4] = ['V5', 5, 4]
    var_df.loc[5] = ['V6', 5, 7]
    var_df.loc[6] = ['V7', 6, 1]
    
    print(var_df)
    print('\n------------\n')
    
    new_var_df = var_df.sort_values(by=['Remaining values', 'Degrees'], inplace=True, ascending=[True, False])
    new_var_df = var_df.reset_index(drop=True)
    print(new_var_df)

When I print out var_df, and new_var_df, they have the same output:

   Variable Remaining values Degrees
0       V1                1       2
1       V2                2       3
2       V3                2       1
3       V4                4       5
4       V6                5       7
5       V5                5       4
6       V7                6       1

This is the output I am expecting after sorting

Expected output after sorting

2
  • 1
    Im not sure if it is a typo or just the same code you are using, but you are creating the new_var_df by sorting var_df, then overwriting the new_var_df by resetting index on var_df, so you are getting the same dataframe. Shouldn't it be new_var_df=new_var_df.reset_index(drop=True)? Commented Oct 31, 2020 at 19:49
  • 1
    @Yashar, thanks a lot. That was the problem. I was resetting the index of the wrong DataFrame(var_df) Commented Nov 1, 2020 at 13:01

2 Answers 2

2

Assign then you do not need inplace

new_var_df = var_df.sort_values(by=['Remaining values', 'Degrees'], ascending=[True, False])
Sign up to request clarification or add additional context in comments.

3 Comments

@David.B look at your original df , it is already order by by=['Remaining values', 'Degrees'], ascending=[True, False]
I dont know if it already doing that. please check the question. i added the output i'm actually looking for
@David.B check you expected output and compared with original df , still the same
0

Thanks @Yashar.

The problem was caused by a typo on my end. I was resetting the index of the wrong DataFrame(var_df), instead of new_var_df.

I solved it by changing this new_var_df = var_df.reset_index(drop=True) to

new_var_df = new_var_df.reset_index(drop=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.