pandas 透视表中文字段排序方法
作者:python_tty 发布时间:2023-11-13 19:11:22
标签:pandas,透视表,排序
前几天有一个需求,透视表中的年级这一列要按照一年级,二年级这样的序列进行排序,但是用过透视表的人都知道,透视表对中文的排序不是太理想,放弃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)
来源:https://blog.csdn.net/python_tty/article/details/80917003
0
投稿
猜你喜欢
- 析构函数:当某个对象成为垃圾或者当对象被显式销毁时执行。GC(Garbage Collector) 在PHP中,没有任何变量指向这个对象时,
- 本文实例讲述了Python实现的多叉树寻找最短路径算法。分享给大家供大家参考,具体如下:多叉树的最短路径:思想: &n
- 方法一、尽量使用复杂的SQL来代替简单的一堆 SQL.同样的事务,一个复杂的SQL完成的效率高于一堆简单SQL完成的效率。有多个查询时,要善
- 本文实例为大家分享了python实现抽奖小程序的具体代码,供大家参考,具体内容如下设计一个抽奖服务 背景:有x个奖品,要求在y天内
- 举例如下:<HTML> <HEAD>  
- 本文实例为大家分享了python实现名片管理系统的具体代码,供大家参考,具体内容如下名片管理系统前提:实现名片管理系统,首先要创建两个pyt
- 简介str是采用Unicode编码方式的序列,主要用于显示bytes是字节序列,主要用于网络和文件传输bytearray和bytes是一样的
- 最近看到了两行 JavaScript 代码,很受启发。1. 封装 DOM 属性在 JavaScript 中,我们可以获取HTML元素的属性值
- 如下所示:import pandas as pd #显示所有列pd.set_option('display.max_columns&
- API的应用通常会处理json数据,刚好今天看到了json字符串和python字符串的区别,放一段代码,区别一下子就看出来,的确json 库
- 如何使用GPU而不是CPU首先查看设备from tensorflow.python.client import device_libprin
- 最近,随着数据库的日益庞大,本来两个差不多的数据库,我开始发现我的数据库查询起来越来越比我朋友网站的慢了,经过初步对照,问题好像出在访问记录
- 实现2048相对来说比较简单,用4*4的二维数组保存地图,pygame.key.get_pressed()获取键盘操作,详见代码。效果图代码
- 代码如下:SELECT [StartDate] FROM [dbo].[udf_Week](2012,2012) WHERE [
- 之前写了一个ajax上传工具。但是只是客户端的工具是我写的,服务器端的那个程序,我是修改了一个网上流传的无组件上传类。因为当时我还不懂什么a
- 错误类型: Microsoft JET Database Engine (0x80004005) 不能使用 '';文件已在使
- 1.元字符: . 它匹配除了换行字符外的任何字符,在 alternate 模式(re.DOTALL)下它甚至可以匹配换行 ^ 匹配行首。除非
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 8 - Input Filtering Part
- Python 异常处理机制还提供了一个 finally 语句,通常用来为 try 块中的程序做扫尾清理工作。注意,和 else 语句不同,f
- 安装部署Scrapy在安装Scrapy前首先需要确定的是已经安装好了Python(目前Scrapy支持Python2.5,Python2.6