Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-place sort_values in pandas what does it exactly mean?

Maybe a very naive question, but I am stuck in this: pandas.Series has a method sort_values and there is an option to do it "in place" or not. I have Googled for it a while, but I am not very clear about it. It seems that this thing is assumed to be perfectly known to everybody but me. Could anyone give me some illustrative explanation how these two options differ each other for dummies...?

Thank you for any assistance.

like image 847
Karel Macek Avatar asked Jan 21 '17 07:01

Karel Macek


People also ask

What does inplace do in sort_values?

inplace = True changes the actual list itself while sorting. inplace = False will return a new sorted list without changing the original. By default, inplace is set to False if unspecified.

What does sort_values in pandas do?

Pandas sort_values() function sorts a data frame in Ascending or Descending order of passed Column. It's different than the sorted Python function since it cannot sort a data frame and particular column cannot be selected. Every parameter has some default values except the 'by' parameter.

What does inplace mean in pandas?

Using the inplace=True keyword in a pandas method changes the default behaviour such that the operation on the dataframe doesn't return anything, it instead 'modifies the underlying data' (more on that later). It mutates the actual object which you apply it to.

What does inplace mean in Python?

Definition - In-place operation is an operation that changes directly the content of a given linear algebra, vector, matrices(Tensor) without making a copy. The operators which helps to do the operation is called in-place operator.


1 Answers

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)
like image 118
Alexey Smirnov Avatar answered Oct 19 '22 05:10

Alexey Smirnov