DataFrame 和 Series 都可以用.sort_index()或.sort_values() 进行排序。
一、sort_values()
def sort_values(self,
axis: Any = 0,
ascending: bool | int | Sequence[bool | int] = True, # ascending = True 默认升序排列;
inplace: bool = False, # If True, perform operation in-place.
kind: str = "quicksort",
na_position: str = "last", # Argument ‘first’ puts NaNs at the beginning, ‘last’ puts NaNs at the end.
ignore_index: bool = False, # If True, the resulting axis will be labeled 0, 1, …, n - 1.
key: (Series) -> Series | ExtensionArray | ndarray | ndarray | Index | None = None)
在原函数上进行修改,inplace = True ,ReturnsSeries ordered by values or None if inplace=True.
按值排序,默认升序, 缺失值放在最上面(na_position = "first")
s = pd.Series([np.nan, 1, 3, 10, 5])
s1 = s.sort_values(na_position='first') # 按值排序,默认升序, 缺失值放在最上面
按值的字符串的小写降序排列(a key function:lambda)
s0 = pd.Series(['a', 'B', 'c', 'D', 'e'])
s2 = s0.sort_values(key=lambda x: x.str.lower(),ascending=False) # 按索引列的字符串的小写降序排列
二、sort_key()
def sort_index(self,
axis: Any = 0, # 与DataFrame兼容所需的参数
level: Any = None, # 指定索引level排序
ascending: bool | int | Sequence[bool | int] = True,
inplace: bool = False,
kind: str = "quicksort", # `快速排序`
na_position: str = "last",
sort_remaining: bool = True,
ignore_index: bool = False,
key: (Index) -> Index | ExtensionArray | ndarray | ndarray | Series | None = None)
1. inplace = True 更新原始Series并返回None,如果 inplace 为False,则返回按标签索引排序的新Series。
2. axis:是与DataFrame兼容所需的参数。其他和 sort_values 类似。
3. na_position = "first"举例如下:
s = pd.Series(['a', 'b', 'c', 'd'], index=[3, 2, 1, np.nan])
s1 = s.sort_index(na_position='first')
print(s1)
4. 按索引列的字符串的小写降序排列(a key function:lambda)
>>> s = pd.Series([1, 2, 3, 4], index=['A', 'b', 'C', 'd'])
>>> s.sort_index(key=lambda x : x.str.lower())
A 1
b 2
C 3
d 4
dtype: int64
5. **指定索引 level 排序(同时多列索引的情况)
>>> 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 8
baz one 6
foo one 4
qux one 2
bar two 7
baz two 5
foo two 3
qux two 1
sort_index(level=1),以第二列索引排序 ,剩余的也排序了。如果换成(level=0),那么结果如下:
bar one 8
two 7
baz one 6
two 5
foo one 4
two 3
qux one 2
two 1
6. 当按照 level 排序时,剩余的 index 的 level 不排序。
s.sort_index(level=1, sort_remaining=False)
qux one 2
foo one 4
baz one 6
bar one 8
qux two 1
foo two 3
baz two 5
bar two 7
dtype: int64