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

作者:python_tty 时间:2023-11-13 19:11:22 

前几天有一个需求,透视表中的年级这一列要按照一年级,二年级这样的序列进行排序,但是用过透视表的人都知道,透视表对中文的排序不是太理想,放弃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

标签:pandas,透视表,排序
0
投稿

猜你喜欢

  • pandas DataFrame索引行列的实现

    2023-12-16 09:52:55
  • Linux下利用python实现语音识别详细教程

    2022-07-13 15:11:55
  • PyTorch小功能之TensorDataset解读

    2023-02-26 06:06:27
  • Python中optparser库用法实例详解

    2023-08-14 01:32:11
  • Python实现的数据结构与算法之基本搜索详解

    2021-07-25 03:47:44
  • 一个可应用在ASP 标记加密文件的MD5的DLL组件

    2008-04-12 07:21:00
  • Go语言中的Slice学习总结

    2023-07-22 23:33:37
  • Python判断一个文件夹内哪些文件是图片的实例

    2023-08-18 01:22:44
  • 面向新手解析python Beautiful Soup基本用法

    2023-11-06 22:29:50
  • Python中if语句的基本格式实例代码

    2023-12-02 14:31:20
  • 对Python中小整数对象池和大整数对象池的使用详解

    2023-02-17 18:13:49
  • python 时间 T 去掉 带上ms 毫秒 时间格式的操作

    2021-12-16 23:24:45
  • Javascript 动画初探(实现)

    2009-02-06 15:56:00
  • python文件操作相关知识点总结整理

    2023-05-09 02:17:42
  • Django中间件实现拦截器的方法

    2022-10-21 04:47:53
  • 如何在社区建立一个寻呼台?

    2009-11-08 18:59:00
  • Python如何实现爬取B站视频

    2021-10-09 07:46:04
  • python文件的读取、写入与删除

    2022-01-21 21:17:12
  • 详解Python:面向对象编程

    2023-10-26 16:17:53
  • pandas group分组与agg聚合的实例

    2023-01-04 14:22:28
  • asp之家 网络编程 m.aspxhome.com