databricks.koalas.DataFrame.sort_values¶
-
DataFrame.
sort_values
(by: Union[Any, List[Any], Tuple, List[Tuple]], ascending: Union[bool, List[bool]] = True, inplace: bool = False, na_position: str = 'last') → Optional[databricks.koalas.frame.DataFrame][source]¶ Sort by the values along either axis.
- Parameters
- bystr or list of str
- ascendingbool or list of bool, default True
Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must match the length of the by.
- inplacebool, default False
if True, perform operation in-place
- na_position{‘first’, ‘last’}, default ‘last’
first puts NaNs at the beginning, last puts NaNs at the end
- Returns
- sorted_objDataFrame
Examples
>>> df = ks.DataFrame({ ... 'col1': ['A', 'B', None, 'D', 'C'], ... 'col2': [2, 9, 8, 7, 4], ... 'col3': [0, 9, 4, 2, 3], ... }, ... columns=['col1', 'col2', 'col3']) >>> df col1 col2 col3 0 A 2 0 1 B 9 9 2 None 8 4 3 D 7 2 4 C 4 3
Sort by col1
>>> df.sort_values(by=['col1']) col1 col2 col3 0 A 2 0 1 B 9 9 4 C 4 3 3 D 7 2 2 None 8 4
Sort Descending
>>> df.sort_values(by='col1', ascending=False) col1 col2 col3 3 D 7 2 4 C 4 3 1 B 9 9 0 A 2 0 2 None 8 4
Sort by multiple columns
>>> df = ks.DataFrame({ ... 'col1': ['A', 'A', 'B', None, 'D', 'C'], ... 'col2': [2, 1, 9, 8, 7, 4], ... 'col3': [0, 1, 9, 4, 2, 3], ... }, ... columns=['col1', 'col2', 'col3']) >>> df.sort_values(by=['col1', 'col2']) col1 col2 col3 1 A 1 1 0 A 2 0 2 B 9 9 5 C 4 3 4 D 7 2 3 None 8 4