Python | Pandas | 单索引 | 自定义排序

Updated: 2022 / 7 / 26


知识点

透视图

pd.pivot_table(df,index=[字段1],values=[字段2],aggfunc=[函数],fill_value=0)

示例数据

# example
pivot = data.pivot_table(
    index='group',                    # index: group
    columns='priority',               # column: priority
    values='Num',                     # value: Num
    aggfunc='count',                  # aggfunc: count
    fill_value='0'                	  # fill_value: fill empty cell with '0'
)
    '''
priority          High  Highest  Low  Medium
group                                       
A                  5.0      7.0    0    36.0
B                  8.0     18.0    0    11.0
N                 94.0     88.0  3.0   217.0
...
    '''

自定义排序

乱序

column

现排序列表
### give a list of reorder of column labels
colOrder = pivot.columns              # <class 'pandas.core.indexes.base.Index'>
	'''
Index(['High', 'Highest', 'Low', 'Medium'], dtype='object', name='priority')
	'''
	
colOrder = pivot.columns.to_list()    # <class 'list'>
	'''
['High', 'Highest', 'Low', 'Medium']
	'''
自定义排序列表
colReorder = colOrder[1:2] + colOrder[0:1] + colOrder[3:4] + colOrder[2:3]
	'''
['Highest', 'High', 'Medium', 'Low']
	'''
自定义排序实现
1.
pivotColReorderA = pivot[colReorder]
    '''
priority          Highest  High  Medium   Low
group
A                     7.0   5.0    36.0     0
B                    18.0   8.0    11.0     0
N                    88.0  94.0   217.0   3.0
...
    '''
2.
pivotColReorderB = pivot.loc[:, colReorder]
    '''
priority          Highest  High  Medium   Low
group
A                     7.0   5.0    36.0     0
B                    18.0   8.0    11.0     0
N                    88.0  94.0   217.0   3.0
...
    '''
3.
###    give a list of reorder of column labels‘ index
### arrange the index of columns in the desired order, e.g. ‘Highest’ is the Nr.1 col and we tend to arrange it to
### Nr.0;
### 'Low' is the Nr.2 col and we tend to put it to Nr.3
pivotColReorderC = pivot.iloc[:, [1, 0, 3, 2]]
    '''
priority          Highest  High  Medium   Low
group
A                     7.0   5.0    36.0     0
B                    18.0   8.0    11.0     0
N                    88.0  94.0   217.0   3.0
...
    '''

row

现排序列表
rowOrder = pivot.index.to_list()  	# <class 'list'>
	'''
['A', 'B', 'N', ...]
	'''
自定义排序列表
rowReorder = rowOrder[0:2] + rowOrder[3:6] + rowOrder[2:3]
	'''
['A', 'B', ..., 'N']
	'''
自定义排序实现
pivot['rowOrder'] = rowOrder
	'''
priority          High  Highest  Low  Medium  rowOrder
group
A                  5.0      7.0    0    36.0         A
B                  8.0     18.0    0    11.0         B
N                 94.0     88.0  3.0   217.0         N
	'''

pivot['rowOrder'] = pivot['rowOrder'].astype('category')
pivot['rowOrder'].cat.reorder_categories(rowReorder, ordered=True, inplace=True)
pivot.sort_values('rowOrder', inplace=True, ascending=True)
	'''
priority          High  Highest  Low  Medium  rowOrder
group
A                  5.0      7.0    0    36.0         A
B                  8.0     18.0    0    11.0         B
...
N                 94.0     88.0  3.0   217.0         N
	'''

pivot.drop(columns='rowOrder', inplace=True)
	'''
priority          High  Highest  Low  Medium
group
A                  5.0      7.0    0    36.0
B                  8.0     18.0    0    11.0
...
N                 94.0     88.0  3.0   217.0
	'''

使用 df.sort()时如果出现报错 AttributeError: 'DataFrame' object has no attribute 'sort'。解决方法为使用 df.sort_values() 作为替代 1


逆序

column

现排序列表
colOrder = pivot.columns                                      # <class 'pandas.core.indexes.base.Index'>
	'''
Index(['High', 'Highest', 'Low', 'Medium'], dtype='object', name='priority')
	'''
	
colOrder = pivot.columns.to_list()                            # <class 'list'>
	'''
['High', 'Highest', 'Low', 'Medium']
	'''
自定义排序列表
# colOrder = colOrder[::-1]
# the same with
colOrder.reverse()
	'''
['Medium', 'Low', 'Highest', 'High']
	'''
自定义排序实现
pivotColReorderD = pivot[colOrder]
	'''
priority           Medium   Low Highest    High
group
A                    36.0    0      7.0     5.0
B                    11.0    0     18.0     8.0
N                   217.0  3.0     88.0    94.0
	...
'''

参考文章

透视图

Pandas | 一文看懂透视表pivot_table

自定义排序实现

Pandas实用技能,将列(column)排序的几种方法
pandas 如何插入一行


  1. pandas DataFrame sort 排序的问题 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值