python将索引升序_使用python根据索引和非索引列对表值进行排序

How can I sort values in a dataframe based on an index and non-indexed columns?

Dataframe:

ID Colour A B C

45356 Green 1 34 4

34455 Yellow 23 0 1

53443 Brown 3 4 3

45555 Green 5 5 2

Table has two index columns (ID and Colour). I will like to sort the table based on ID(ascending), A (Descending) and C (ascending).

Required output is :

ID Colour A B C

34455 Yellow 23 0 1

45356 Green 1 34 4

45555 Green 5 5 2

53443 Brown 3 4 3

I have tried this:

df.set_index(inplace=True)

df.sort_values(['ID', 'A', 'C'], ascending=['True','False','True'])

This didn't work as "ID" as a column was not recognized.

解决方案

you want

df.reset_index().sort_values(

['ID', 'A', 'C'],

ascending=['True','False','True']

).set_index(['ID', 'Colour'])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值