pythonsortingpandasin-place

What does In-place sort_values in pandas exactly mean?


I am stuck in: pandas.Series has a method sort_values and there is an option to do it "in place" or not. Could anyone give me some illustrative explanation how these two options differ each other?


Solution

  • Here an example. df1 will hold sorted dataframe and df will be intact

    import pandas as pd
    from datetime import datetime as dt
    df = pd.DataFrame(data=[22,22,3],
                      index=[dt(2016, 11, 10, 0), dt(2016, 11, 10, 13), dt(2016, 11, 13, 5)],
                      columns=['foo'])
    
    df1 = df.sort_values(by='foo')
    print(df, df1)
    

    In the case below, df will hold sorted values

    import pandas as pd
    from datetime import datetime as dt
    
    df = pd.DataFrame(data=[22,22,3],
                      index=[dt(2016, 11, 10, 0), dt(2016, 11, 10, 13), dt(2016, 11, 13, 5)],
                      columns=['foo'])
    
    df.sort_values(by='foo', inplace=True)
    print(df)