Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.sort_index#

Series.sort_index(*,axis=0,level=None,ascending=True,inplace=False,kind='quicksort',na_position='last',sort_remaining=True,ignore_index=False,key=None)[source]#

Sort Series by index labels.

Returns a new Series sorted by label ifinplace argument isFalse, otherwise updates the original series and returns None.

Parameters:
axis{0 or ‘index’}

Unused. Parameter needed for compatibility with DataFrame.

levelint, optional

If not None, sort on values in specified index level(s).

ascendingbool or list-like of bools, default True

Sort ascending vs. descending. When the index is a MultiIndex thesort direction can be controlled for each level individually.

inplacebool, default False

If True, perform operation in-place.

kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’

Choice of sorting algorithm. See alsonumpy.sort() for moreinformation. ‘mergesort’ and ‘stable’ are the only stable algorithms. ForDataFrames, this option is only applied when sorting on a singlecolumn or label.

na_position{‘first’, ‘last’}, default ‘last’

If ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end.Not implemented for MultiIndex.

sort_remainingbool, default True

If True and sorting by level and index is multilevel, sort by otherlevels too (in order) after sorting by specified level.

ignore_indexbool, default False

If True, the resulting axis will be labeled 0, 1, …, n - 1.

keycallable, optional

If not None, apply the key function to the index valuesbefore sorting. This is similar to thekey argument in thebuiltinsorted() function, with the notable difference thatthiskey function should bevectorized. It should expect anIndex and return anIndex of the same shape.

Returns:
Series or None

The original Series sorted by the labels or None ifinplace=True.

See also

DataFrame.sort_index

Sort DataFrame by the index.

DataFrame.sort_values

Sort DataFrame by the value.

Series.sort_values

Sort Series by the value.

Examples

>>>s=pd.Series(['a','b','c','d'],index=[3,2,1,4])>>>s.sort_index()1    c2    b3    a4    ddtype: object

Sort Descending

>>>s.sort_index(ascending=False)4    d3    a2    b1    cdtype: object

By default NaNs are put at the end, but usena_position to placethem at the beginning

>>>s=pd.Series(['a','b','c','d'],index=[3,2,1,np.nan])>>>s.sort_index(na_position='first')NaN     d 1.0    c 2.0    b 3.0    adtype: object

Specify index level to sort

>>>arrays=[np.array(['qux','qux','foo','foo',...'baz','baz','bar','bar']),...np.array(['two','one','two','one',...'two','one','two','one'])]>>>s=pd.Series([1,2,3,4,5,6,7,8],index=arrays)>>>s.sort_index(level=1)bar  one    8baz  one    6foo  one    4qux  one    2bar  two    7baz  two    5foo  two    3qux  two    1dtype: int64

Does not sort by remaining levels when sorting by levels

>>>s.sort_index(level=1,sort_remaining=False)qux  one    2foo  one    4baz  one    6bar  one    8qux  two    1foo  two    3baz  two    5bar  two    7dtype: int64

Apply a key function before sorting

>>>s=pd.Series([1,2,3,4],index=['A','b','C','d'])>>>s.sort_index(key=lambdax:x.str.lower())A    1b    2C    3d    4dtype: int64

[8]ページ先頭

©2009-2025 Movatter.jp