python将一组数据排序_Python 数据处理(十一)—— 排序

11 排序

pandas 支持三种排序方式:

按索引排序

按指定列的值排序

按索引和列排序

11.1 按索引排序

Series.sort_index() 和 DataFrame.sort_index() 方法用于按其索引级别对 pandas 对象进行排序

In [300]: df = pd.DataFrame(

.....: {

.....: "one": pd.Series(np.random.randn(3), index=["a", "b", "c"]),

.....: "two": pd.Series(np.random.randn(4), index=["a", "b", "c", "d"]),

.....: "three": pd.Series(np.random.randn(3), index=["b", "c", "d"]),

.....: }

.....: )

.....:

In [301]: unsorted_df = df.reindex(

.....: index=["a", "d", "c", "b"], columns=["three", "two", "one"]

.....: )

.....:

In [302]: unsorted_df

Out[302]:

three two one

a NaN -1.152244 0.562973

d -0.252916 -0.109597 NaN

c 1.273388 -0.167123 0.640382

b -0.098217 0.009797 -1.299504

# DataFrame

In [303]: unsorted_df.sort_index()

Out[303]:

three two one

a NaN -1.152244 0.562973

b -0.098217 0.009797 -1.299504

c 1.273388 -0.167123 0.640382

d -0.252916 -0.109597 NaN

In [304]: unsorted_df.sort_index(ascending=False)

Out[304]:

three two one

d -0.252916 -0.109597 NaN

c 1.273388 -0.167123 0.640382

b -0.098217 0.009797 -1.299504

a NaN -1.152244 0.562973

In [305]: unsorted_df.sort_index(axis=1)

Out[305]:

one three two

a 0.562973 NaN -1.152244

d NaN -0.252916 -0.109597

c 0.640382 1.273388 -0.167123

b -1.299504 -0.098217 0.009797

# Series

In [306]: u

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值