mars.dataframe.DataFrame.sort_values#

DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, parallel_kind='PSRS', psrs_kinds=None)#

按任一轴上的值排序。

Parameters
  • df (Mars DataFrame) – 输入数据框。

  • (str) – 用于排序的名称或名称列表。

  • (%(axes_single_arg)s, 默认 0) – 要排序的轴。

  • ascending (boollistbool, 默认为 True) – 排序方式为升序或降序。 若要多重排序,请指定列表。 如果这是一个布尔值列表,必须与 by 的长度一致。

  • inplace (bool, 默认值为 False) – 如果为 True,则在原地执行操作。

  • kind ({'quicksort', 'mergesort', 'heapsort'}, default 'quicksort') – 排序算法的选择。有关更多信息,请参阅 ndarray.np.sort。 mergesort 是唯一的稳定算法。对于 DataFrames,此选项仅在对单列或标签进行排序时应用。

  • na_position ({'first', 'last'}, default 'last') – 如果first,则将NaNs放在开头;last将NaNs放在 结尾。

  • ignore_index (bool, default False) – 如果为 True,结果轴将标记为 0, 1, …, n - 1。

  • parallel_kind ({'PSRS'}, 默认 'PSRS') – 并行排序算法,详情请参阅: http://csweb.cs.wfu.edu/bigiron/LittleFE-PSRS/build/html/PSRSalgorithm.html

Returns

sorted_obj – 如果inplace=False,则为排序后的值的DataFrame,否则为None。

Return type

DataFrame 或 无

示例

>>> import mars.dataframe as md
>>> df = md.DataFrame({
...     'col1': ['A', 'A', 'B', np.nan, 'D', 'C'],
...     'col2': [2, 1, 9, 8, 7, 4],
...     'col3': [0, 1, 9, 4, 2, 3],
... })
>>> df.execute()
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
3   NaN  8    4
4   D    7    2
5   C    4    3

按col1排序

>>> df.sort_values(by=['col1']).execute()
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4

按多个列排序

>>> df.sort_values(by=['col1', 'col2']).execute()
    col1 col2 col3
1   A    1    1
0   A    2    0
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4

降序排序

>>> df.sort_values(by='col1', ascending=False).execute()
    col1 col2 col3
4   D    7    2
5   C    4    3
2   B    9    9
0   A    2    0
1   A    1    1
3   NaN  8    4

将NAs放在首位

>>> df.sort_values(by='col1', ascending=False, na_position='first').execute()
    col1 col2 col3
3   NaN  8    4
4   D    7    2
5   C    4    3
2   B    9    9
0   A    2    0
1   A    1    1