bigframes.pandas.DataFrame.sort_values#

DataFrame.sort_values(by: str | Sequence[str], *, inplace: Literal[False] = False, ascending: bool | Sequence[bool] = True, kind: str = 'quicksort', na_position: Literal['first', 'last'] = 'last') DataFrame[source]#
DataFrame.sort_values(by: str | Sequence[str], *, inplace: Literal[True] = False, ascending: bool | Sequence[bool] = True, kind: str = 'quicksort', na_position: Literal['first', 'last'] = 'last') None

Sort by the values along row axis.

Examples:

>>> df = bpd.DataFrame({
...     'col1': ['A', 'A', 'B', pd.NA, '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  <NA>     8     4    D
4     D     7     2    e
5     C     4     3    F

[6 rows x 4 columns]

Sort by col1:

>>> 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  <NA>     8     4    D

[6 rows x 4 columns]

Sort by multiple columns:

>>> 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  <NA>     8     4    D

[6 rows x 4 columns]

Sort Descending:

>>> 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  <NA>     8     4    D

[6 rows x 4 columns]

Putting NAs first:

>>> df.sort_values(by='col1', ascending=False, na_position='first')
   col1  col2  col3 col4
3  <NA>     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

[6 rows x 4 columns]
Parameters:
  • by (str or Sequence[str]) – Name or list of names to sort by.

  • ascending (bool or Sequence[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.

  • inplace (bool, default False) – If True, perform operation in-place.

  • kind (str, default 'quicksort') – Choice of sorting algorithm. Accepts ‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’. Ignored except when determining whether to sort stably. ‘mergesort’ or ‘stable’ will result in stable reorder.

  • na_position ({‘first’, ‘last’}, default last) – {'first', 'last'}, default ‘last’ Puts NaNs at the beginning if first; last puts NaNs at the end.

Returns:

DataFrame with sorted values or None if inplace=True.

Return type:

bigframes.pandas.DataFram or None

Raises:

ValueError – If value of na_position is not one of first or last.