Why does my Pandas DataFrame not display the new order using `sort_values`?

New to Pandas, maybe I am missing a big idea? I have a Pandas DataFrame transaction register with a form like (500,4) :

 Time datetime64[ns] Net Total float64 Tax float64 Total Due float64 

I am working on my code in a Python3 Jupyter laptop . I can not go through sorting any column. Working with various code examples for sorting, I don't see the output order when I check df. So, I reduced the problem by trying to order only one column:

 df.sort_values(by='Time') # OR df.sort_values(['Total Due']) # OR df.sort_values(['Time'], ascending=True) 

No matter what column name or boolean argument I use, the displayed results never change order.

Thinking it might be Jupyter, I looked at the results with print(df) , df.head() and HTML(df.to_html()) (last example for Jupyter laptops). I also restarted the entire notepad from importing CSV to this code. And I'm also new to Python3 (from version 2.7), so sometimes I get stuck, but I don’t see how relevant this is in this case.

Another publication has a similar problem, Python Pandas sort_values ​​dataframe does not work . In this case, the ordering was performed by the type of the string column. But, as you can see, all the columns here are uniquely sorted.

Why doesn't my Pandas DataFrame display the new order with sort_values ?

+14
source share
2 answers

df.sort_values(['Total Due']) returns a sorted DF, but it does not update the DF in place.

So do it explicitly:

 df = df.sort_values(['Total Due']) 

or

 df.sort_values(['Total Due'], inplace=True) 
+41
source

My problem, for your information, was that I did not return the received data frame, so PyCharm did not bother to update the specified data frame. Naming the data frame after the return keyword resolved the issue.

Edit: I had return at the end of my method instead of return df , which the debugger should have noticed because df not updating, despite my explicit sorting in place.

0
source

Source: https://habr.com/ru/post/1265067/


All Articles