pandas 透视表中文字段排序方法

yipeiwu_com5年前Python基础

前几天有一个需求,透视表中的年级这一列要按照一年级,二年级这样的序列进行排序,但是用过透视表的人都知道,透视表对中文的排序不是太理想,放弃pandas自带的排序方法。测试了很久,想到一个办法。先把dataframe中需要特殊排序的列中的汉字转换成数字,然后生成透视表,生成透视表之后,再把透视表的index或者columns中的数字替换成相应的汉字,透视表的结果就会按照你想要的顺序进行排序。

 def get_special_sort_data(self, groupby, columns):
 # 获取需要特殊处理的字段的信息
 special_sort_cols = None
 cols_in_index_or_column = None # 判断特殊排序字段在index还是column中
 if self.datasource.has_special_sort_cols:
  # 获取表需要处理的特殊字段信息
  special_sort_cols = self.datasource.get_sort_columns() # {"grade_name": {}}

  if special_sort_cols:
  i_intersection = list(set(groupby) & set(special_sort_cols.keys()))
  c_intersection = list(set(columns) & set(special_sort_cols.keys()))
  if i_intersection:
   cols_in_index_or_column = ('index', i_intersection)
  elif c_intersection:
   cols_in_index_or_column = ('column', c_intersection)
 return cols_in_index_or_column, special_sort_cols
cols_in_index_or_column, special_sort_cols = self.get_special_sort_data(groupby, columns) # special_sort_cols:{"grade_name": {"一年级": 1, "二年级":2, "三年级": 3 ....}}

if cols_in_index_or_column:
 for col in cols_in_index_or_column[1]:
 df[col] = df[col].replace(special_sort_cols.get(col)) # 替换df

# 获取透视表
if cols_in_index_or_column:
 if cols_in_index_or_column[0] == 'index':
  if len(groupby) == 1:
   col_name = cols_in_index_or_column[1][0]
   sort_info = special_sort_cols.get(col_name)
   r_sort_info = {v:k for k, v in zip(sort_info.keys(), sort_info.values())}
   index_1 = df.index.tolist()
   index_1 = [r_sort_info.get(item) for item in index_1]
   df.index = Index(index_1, name=df.index.name)
  else:
   for item in cols_in_index_or_column[1]:
   ix = df.index.names.index(item)
   index_1 = df.index.levels[ix].tolist()
   sort_info = special_sort_cols.get(item)
   r_sort_info = {v: k for k, v in zip(sort_info.keys(), sort_info.values())}
   index_1 = [r_sort_info.get(item) for item in index_1]
   df.index = df.index.set_levels(index_1, level=ix)
 else:
  for item in cols_in_index_or_column[1]:
  ix = df.columns.names.index(item)
  col_1 = df.columns.levels[ix].tolist()
  sort_info = special_sort_cols.get(item)
  r_sort_info = {v: k for k, v in zip(sort_info.keys(), sort_info.values())}
  col_1 = [r_sort_info.get(item) for item in col_1]
  df.columns = df.columns.set_levels(col_1, level=ix)

以上这篇pandas 透视表中文字段排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

梯度下降法介绍及利用Python实现的方法示例

梯度下降法介绍及利用Python实现的方法示例

本文主要给大家介绍了梯度下降法及利用Python实现的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧。 梯度下降法介绍 梯度下降法(gradient descen...

使用Python生成url短链接的方法

几乎所有的微薄都提供了缩短网址的服务,其原理就是将一个url地址按照一定的算法生成一段字符串,然后加在一个短域名后面边成了一个新的url地址,数据库中会存放这个短地址和原始的地址,当用户...

在Mac下使用python实现简单的目录树展示方法

在Linux或者Windows下想要查看目录树都可以通过tree命令来实现,两个操作系统中的操作也很相似。使用Linux时,最初以为这是shell中都有这个命令可用。结果使用Mac的时候...

Python中logging实例讲解

Python中logging实例讲解

logging 的基本用法网上很多,这里就不介绍了。在引入正文之前,先来看一个需求: 假设需要将某功能封装成类库供他人使用,如何处理类库中的日志? 数年前在一个 C# 开发的项目中,我用...

python中的decimal类型转换实例详解

[Python标准库]decimal——定点数和浮点数的数学运算         作用:使用定点数和浮点数的小数运算...