前几天有一个需求,透视表中的年级这一列要按照一年级,二年级这样的序列进行排序,但是用过透视表的人都知道,透视表对中文的排序不是太理想,放弃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 透视表中文字段排序方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
pandas,透视表,中文排序
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“pandas 透视表中文字段排序方法”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月10日
2024年10月10日
- 刘德华.1989-永远...记得你(2012金碟复刻版)【EMI百代】【WAV+CUE】
- 《维纳斯璀璨假期死或生Xtreme》游戏特色玩法
- 《完蛋我被美女包围了前传》角色介绍
- 2024年免费游戏加速器推荐,稳定才是王道!
- 《蛋仔派对》小海鹦兰兰皮肤图鉴
- 《蛋仔派对》憨憨小六皮肤图鉴
- 《蛋仔派对》打工小电视皮肤图鉴
- 叶启田2006-人生[台湾首版][WAV+CUE]
- [雨果唱片]陈百强2012《同名专辑LPCD45》[WAV+CUE]
- 安七炫2005-面具(中文版)[SM娱乐][WAV+CUE]
- 曝AMD曾有机会为Switch2提供芯片 却因能耗错失机会
- 《塞尔达传说:智慧的再现》新作上市宣传片公开 M站86分
- 宫本茂谈AI技术应用:任天堂更愿意走不同的路
- 《蔡琴 不了情 经典歌曲香港演唱会2CD》[ISO][6.6GB]
- 《萧煌奇 说故事的歌 2022》[FLAC/分轨][380MB]