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

52,543

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

So do it explicitly:

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

or

df.sort_values(['Total Due'], inplace=True)
Share:
52,543
xtian
Author by

xtian

Updated on September 26, 2020

Comments

  • xtian
    xtian over 3 years

    New to Pandas, so maybe I'm missing a big idea? I have a Pandas DataFrame of register transactions with shape like (500,4):

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

    I'm working through my code in a Python3 Jupyter notebook. I can't get past sorting any column. Working through the different code examples for sort, I'm not seeing the output reorder when I inspect the df. So, I've reduced the problem to trying to order just one column:

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

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

    Thinking it could be a Jupyter thing, I've previewed the results using print(df), df.head(), and HTML(df.to_html()) (the last example is for Jupyter notebooks). I've also rerun the whole notebook from import CSV to this code. And, I'm also new to Python3 (from 2.7), so I get stuck with that sometimes, but I don't see how that's relevant in this case.

    Another post has a similar problem, Python pandas dataframe sort_values does not work. In that instance, the ordering was on a column type string. But as you can see all of the columns here are unambiguously sortable.

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

  • r_hudson
    r_hudson over 4 years
    I am using PyCharm and facing the same problem. I tried naming dataframe at several different steps but can not solve this issue. Could please give more specifics about how you resolved your problem.
  • gherson
    gherson over 4 years
    @r_hudson I added an Edit.
  • r_hudson
    r_hudson over 4 years
    Thanks @gherson for the explanation. However, I wasted some 2 hours today and my issue is still not resolved. I am sorting along axis=1 and also specify by = column_names and for some reason dataframe is not sorting. Surprisingly it was working fine yesterday.
  • gherson
    gherson over 4 years
    I tell my students to get a trivial instance working then divide and conquer: add ~half of the non-working code and, depending if it still works, add or subtract code, until the point of contention is isolated, and tweaked until working.
  • DesiKeki
    DesiKeki over 3 years
    @r_hudson I was in the same boat as yours. In my case the issue came out to be that although the values of my column seemed to be numeric, but they were not. So the following trick worked for me: df["time"] = pd.to_numeric(df["time"]) df.sort_values(by = ['time'], ascending = True, inplace=True)