【Pandas】sort_values() / sort_index()

文章介绍了Pandas中Series的.sort_values()和.sort_index()方法,用于按值或按索引排序。sort_values()支持按值升序或降序排序,可调整缺失值位置,示例展示了按字符串小写排序。sort_index()则用于按索引排序,包括指定level排序和控制剩余索引是否排序。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值