欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年11月16日 09:25:06   作者:python_tty  
今天小編就為大家分享一篇pandas 透視表中文字段排序方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

前幾天有一個(gè)需求,透視表中的年級(jí)這一列要按照一年級(jí),二年級(jí)這樣的序列進(jìn)行排序,但是用過(guò)透視表的人都知道,透視表對(duì)中文的排序不是太理想,放棄pandas自帶的排序方法。測(cè)試了很久,想到一個(gè)辦法。先把dataframe中需要特殊排序的列中的漢字轉(zhuǎn)換成數(shù)字,然后生成透視表,生成透視表之后,再把透視表的index或者columns中的數(shù)字替換成相應(yīng)的漢字,透視表的結(jié)果就會(huì)按照你想要的順序進(jìn)行排序。

 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": {"一年級(jí)": 1, "二年級(jí)":2, "三年級(jí)": 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 透視表中文字段排序方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 分享十個(gè)Python超級(jí)好用提高工作效率的自動(dòng)化腳本

    分享十個(gè)Python超級(jí)好用提高工作效率的自動(dòng)化腳本

    在這個(gè)自動(dòng)化時(shí)代,我們有很多重復(fù)無(wú)聊的工作要做。?想想這些你不再需要一次又一次地做的無(wú)聊的事情,讓它自動(dòng)化,讓你的生活更輕松。本文分享了10個(gè)Python自動(dòng)化腳本,希望對(duì)大家有所幫助
    2022-11-11
  • python使用webbrowser瀏覽指定url的方法

    python使用webbrowser瀏覽指定url的方法

    這篇文章主要介紹了python使用webbrowser瀏覽指定url的方法,實(shí)例分析了Python操作webbrowser模塊的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-04-04
  • Pycharm 2020年最新激活碼(親測(cè)有效)

    Pycharm 2020年最新激活碼(親測(cè)有效)

    本文給大家分享Pycharm 2020年最新激活碼,親測(cè)試過(guò)可以放心使用,感興趣的朋友快來(lái)吧
    2020-01-01
  • Python字典實(shí)現(xiàn)簡(jiǎn)單的三級(jí)菜單(實(shí)例講解)

    Python字典實(shí)現(xiàn)簡(jiǎn)單的三級(jí)菜單(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇Python字典實(shí)現(xiàn)簡(jiǎn)單的三級(jí)菜單(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Python的多態(tài)性實(shí)例分析

    Python的多態(tài)性實(shí)例分析

    這篇文章主要介紹了Python的多態(tài)性,以實(shí)例形式深入淺出的分析了Python在面向?qū)ο缶幊讨卸鄳B(tài)性的原理與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-07-07
  • Python發(fā)送郵件的幾種方式(最全總結(jié)!)

    Python發(fā)送郵件的幾種方式(最全總結(jié)!)

    發(fā)送電子郵件是個(gè)很常見(jiàn)的開(kāi)發(fā)需求,平時(shí)如果有什么重要的信息怕錯(cuò)過(guò),就可以發(fā)個(gè)郵件到郵箱來(lái)提醒自己,這篇文章主要給大家介紹了關(guān)于Python發(fā)送郵件的幾種方式,需要的朋友可以參考下
    2024-03-03
  • 基于Python實(shí)現(xiàn)文件處理程序

    基于Python實(shí)現(xiàn)文件處理程序

    這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件處理程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Flask框架URL管理操作示例【基于@app.route】

    Flask框架URL管理操作示例【基于@app.route】

    這篇文章主要介紹了Flask框架URL管理操作,結(jié)合實(shí)例形式分析了@app.route進(jìn)行URL控制的相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • 基于python模擬bfs和dfs代碼實(shí)例

    基于python模擬bfs和dfs代碼實(shí)例

    這篇文章主要介紹了基于python模擬bfs和dfs代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換

    python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09

最新評(píng)論