sort_values(self, by, axis: 'Axis' = 0, ascending=True, inplace: 'bool' = False, kind: 'str' = 'quicksort', na_position: 'str' = 'last', ignore_index: 'bool' = False, key: 'ValueKeyFunc' = None)
Name or list of names to sort by.
if :None:None:`axis`
is 0 or :None:None:`'index'`
then :None:None:`by`
may contain index levels and/or column labels.
if :None:None:`axis`
is 1 or :None:None:`'columns'`
then :None:None:`by`
may contain column levels and/or index labels.
Axis to be sorted.
Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
If True, perform operation in-place.
Choice of sorting algorithm. See also numpy.sort
for more information. :None:None:`mergesort`
and :None:None:`stable`
are the only stable algorithms. For DataFrames, this option is only applied when sorting on a single column or label.
Puts NaNs at the beginning if :None:None:`first`
; :None:None:`last`
puts NaNs at the end.
If True, the resulting axis will be labeled 0, 1, …, n - 1.
Apply the key function to the values before sorting. This is similar to the :None:None:`key`
argument in the builtin sorted
function, with the notable difference that this :None:None:`key`
function should be vectorized. It should expect a Series
and return a Series with the same shape as the input. It will be applied to each column in :None:None:`by`
independently.
DataFrame with sorted values or None if inplace=True
.
Sort by the values along either axis.
DataFrame.sort_index
Sort a DataFrame by the index.
Series.sort_values
Similar method for a Series.
>>> df = pd.DataFrame({
... 'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],
... 'col2': [2, 1, 9, 8, 7, 4],
... 'col3': [0, 1, 9, 4, 2, 3],
... 'col4': ['a', 'B', 'c', 'D', 'e', 'F']
... })
... df col1 col2 col3 col4 0 A 2 0 a 1 A 1 1 B 2 B 9 9 c 3 NaN 8 4 D 4 D 7 2 e 5 C 4 3 F
Sort by col1
This example is valid syntax, but we were not able to check execution>>> df.sort_values(by=['col1']) col1 col2 col3 col4 0 A 2 0 a 1 A 1 1 B 2 B 9 9 c 5 C 4 3 F 4 D 7 2 e 3 NaN 8 4 D
Sort by multiple columns
This example is valid syntax, but we were not able to check execution>>> df.sort_values(by=['col1', 'col2']) col1 col2 col3 col4 1 A 1 1 B 0 A 2 0 a 2 B 9 9 c 5 C 4 3 F 4 D 7 2 e 3 NaN 8 4 D
Sort Descending
This example is valid syntax, but we were not able to check execution>>> df.sort_values(by='col1', ascending=False) col1 col2 col3 col4 4 D 7 2 e 5 C 4 3 F 2 B 9 9 c 0 A 2 0 a 1 A 1 1 B 3 NaN 8 4 D
Putting NAs first
This example is valid syntax, but we were not able to check execution>>> df.sort_values(by='col1', ascending=False, na_position='first') col1 col2 col3 col4 3 NaN 8 4 D 4 D 7 2 e 5 C 4 3 F 2 B 9 9 c 0 A 2 0 a 1 A 1 1 B
Sorting with a key function
This example is valid syntax, but we were not able to check execution>>> df.sort_values(by='col4', key=lambda col: col.str.lower()) col1 col2 col3 col4 0 A 2 0 a 1 A 1 1 B 2 B 9 9 c 3 NaN 8 4 D 4 D 7 2 e 5 C 4 3 F
Natural sort with the key argument, using the natsort package.
This example is valid syntax, but we were not able to check execution>>> df = pd.DataFrame({This example is valid syntax, but we were not able to check execution
... "time": ['0hr', '128hr', '72hr', '48hr', '96hr'],
... "value": [10, 20, 30, 40, 50]
... })
... df time value 0 0hr 10 1 128hr 20 2 72hr 30 3 48hr 40 4 96hr 50
>>> from natsort import index_natsortedSee :
... df.sort_values(
... by="time",
... key=lambda x: np.argsort(index_natsorted(df["time"]))
... ) time value 0 0hr 10 3 48hr 40 2 72hr 30 4 96hr 50 1 128hr 20
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.frame.DataFrame.nsmallest
pandas.core.indexes.base.Index.sort_values
pandas.core.series.Series.sort_index
pandas.core.series.Series.sort_values
pandas.core.frame.DataFrame.nlargest
pandas.core.frame.DataFrame.sort_index
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them