pyspark.pandas.DataFrame.sort_index#
- DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind=None, na_position='last', ignore_index=False)[source]#
Sort object by labels (along an axis)
- Parameters
- axisindex, columns to direct sorting. Currently, only axis = 0 is supported.
- levelint or level name or list of ints or list of level names
if not None, sort on values in specified index level(s)
- ascendingboolean, default True
Sort ascending vs. descending
- inplacebool, default False
if True, perform operation in-place
- kindstr, default None
pandas-on-Spark does not allow specifying the sorting algorithm now, default None
- na_position{‘first’, ‘last’}, default ‘last’
first puts NaNs at the beginning, last puts NaNs at the end. Not implemented for MultiIndex.
- ignore_indexbool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.
New in version 3.4.0.
- Returns
- sorted_objDataFrame
Examples
>>> df = ps.DataFrame({'A': [2, 1, np.nan]}, index=['b', 'a', np.nan])
>>> df.sort_index() A a 1.0 b 2.0 None NaN
>>> df.sort_index(ascending=False) A b 2.0 a 1.0 None NaN
>>> df.sort_index(na_position='first') A None NaN a 1.0 b 2.0
>>> df.sort_index(ignore_index=True) A 0 1.0 1 2.0 2 NaN
>>> df.sort_index(inplace=True) >>> df A a 1.0 b 2.0 None NaN
>>> df = ps.DataFrame({'A': range(4), 'B': range(4)[::-1]}, ... index=[['b', 'b', 'a', 'a'], [1, 0, 1, 0]], ... columns=['A', 'B'])
>>> df.sort_index() A B a 0 3 0 1 2 1 b 0 1 2 1 0 3
>>> df.sort_index(level=1) A B b 0 1 2 a 0 3 0 b 1 0 3 a 1 2 1
>>> df.sort_index(level=[1, 0]) A B a 0 3 0 b 0 1 2 a 1 2 1 b 1 0 3
>>> df.sort_index(ignore_index=True) A B 0 3 0 1 2 1 2 1 2 3 0 3