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
投稿
猜你喜欢
- 1、按位取反bitwise_not()按位取反就是将数值根据每个bit位1变0,0变1,比如0xf0按位取反就变成了0x0f,如果是uint
- panic源码解读前言本文是在go version go1.13.15 darwin/amd64上进行的panic的作用panic能够改变程
- 过年GUI博客二连发,本打算出去玩玩,奈何空气,天气实在差,遂使用tkinter开发一款GUI刷屏器,写此博客记录一下我的开发思路。一.准备
- python 的语法定义和C++、matlab、java 还是很有区别的。1. 括号与函数调用def devided_3(x):  
- 1. mysql的md5 mysql存在系统函数md5(“xxxxx”);2. mssql的md5&nb
- 前言分析目标网站的登录方式目标地址: https://github.com/login 登录方式做出分析:
- 1.Fork出来的Git仓库同步代码背景:有的时候从原仓库fork出了一个新仓库,这个新仓库做了自己的修改。可是原仓库也进行了更新,比如修复
- 一、前期准备CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT,
- 背景pytorch作为深度学习的计算框架正得到越来越多的应用.我们除了在模型训练阶段应用外,最近也把pytorch应用在了部署上.在部署时,
- 本文实例讲述了Java使用正则表达式截取重复出现的XML字符串功能。分享给大家供大家参考,具体如下:public static void m
- html代码:<!DOCTYPE html><html lang="en"><head&g
- fixHtmlTag version 0.2 这个版本解决了上次遗留的问题,即就近闭合和嵌套闭合问题。具体可以看代码的注释。 <?ph
- 前言相信在日常生活中,平常大家聚在一起总会聊聊天,特别是女生(有冒犯到doge)非常喜欢聊星座,这个男生什么星座呀,那个男生什么星座呀…今天
- 模块导入1.1 import导入模块所谓的模块其实就是一个外部的工具包,其中存在的其实就是Python文件,这些文件都实现了某种特定的功能,
- 在使用django-rest-framework开发项目的时候我们总是避免不了跨域的问题,因为现在大多数的项目都是前后端分离,前后端项目部署
- 在Pytorch中,torch.utils.data中的Dataset与DataLoader是处理数据集的两个函数,用来处理加载数据集。通常
- 在list中嵌套元组,在进行sort排序的时候,产生的是原数组的副本,排序过程中,先根据第一个字段进行从小到大排序,如果第一个字段相同的话,
- 本文实例讲述了python通过wxPython打开一个音频文件并播放的方法。分享给大家供大家参考。具体如下:这段代码片段使用wx.lib.f
- 在select语句中可以使用groupby子句将行划分成较小的组,然后,使用聚组函数返回每一个组的汇总信息,另外,可以使用having子句限
- 首先选择操作系统。由于ASP属于MS(Microsoft)的东西,所以我们要选择MS的操作系统,Windows 98以上就可以(