In place of sort_values ​​in pandas, what does this mean?

It may be a very naive question, but I am stuck with this: pandas.Series has a sort_values ​​method, and it is possible to do it "in place" or not. I have Googled for this for a while, but I am not very clear about this. It seems that this thing is considered well known to everyone except me. Can someone give me an illustrative explanation of how these two options differ from each other? For Dummies...

Thanks for any help.

+4
source share
2 answers

Here is an example. df1will contain a sorted data file, and dfwill 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)

df

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)
+2

sort_values ​​document, . , .

:

import numpy as np
import pandas as pd

s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
print(s)
a   -0.872271
b    0.294317
c   -0.017433
d   -1.375316
e    0.993197
dtype: float64

s_sorted = s.sort_values()

print(s_sorted)

d   -1.375316
a   -0.872271
c   -0.017433
b    0.294317
e    0.993197
dtype: float64

print(id(s_sorted))
127952880

print(id(s))
127724792

So s s_sorted - . inplace = True.

s.sort_values(inplace=True)
print(s)
d   -1.375316
a   -0.872271
c   -0.017433
b    0.294317
e    0.993197
dtype: float64

print(id(s))
127724792

, , .

+3

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


All Articles