【问题标题】:Level-Wise Sorting of Series in PandasPandas 中系列的按级别排序
【发布时间】:2023-03-18 14:46:01
【问题描述】:

我有一个名为 mydf 的数据框

mydf

我执行了以下操作,然后将其转换为系列。

mydf.groupby([mydf.type,mydf.name]).size()

现在我有一个具有两个类型的系列,即演员和女演员。

    type      name               
    actor    'Big' Ben Moroz        1
             'Ducky' Louie          3
             'Fast' Eddie Mahler    1
             'King Kong' Kashey     1
             'Muddy' Berry          1

    actress   Zedra Conde           3
              Zena Marshall         1
              Zinaida Morskaya      1
              Zoe Holland           1
              Zoia Karabanova       2

现在我希望我的结果在 level 演员中按降序排序,如果演员 "value"(在第三个未命名的 say 列中给出)相同,那么排序必须由 "name" 完成,然后在另一个称为女演员的 level 中进行排序时必须遵循相同的模式

type      name               
actor    'Ducky' Louie          3
         'Big' Ben Moroz        1
         'Fast' Eddie Mahler    1
         'King Kong' Kashey     1
         'Muddy' Berry          1

actress   Zedra Conde           3
          Zoia Karabanova       2
          Zena Marshall         1
          Zinaida Morskaya      1
          Zoe Holland           1

注意:- 请避免循环。

【问题讨论】:

    标签: python python-3.x sorting pandas series


    【解决方案1】:

    不幸的是,我想出的所有东西都需要双重分组/排序。假设我们有一个 DataFrame

    import pandas as pd
    import numpy as np
    import random
    
    d = pd.DataFrame({'type': ['actor']*5+['actress']*5,  
                      'name' : [random.choice(['a', 'b', 'c']) for i in range(10)]})
    d
    
    
        name    type
    0   c   actor
    1   c   actor
    2   a   actor
    3   b   actor
    4   a   actor
    5   c   actress
    6   c   actress
    7   c   actress
    8   a   actress
    9   a   actress
    
    
    d.groupby([d.type,d.name]).size()
    
    type     name
    actor    a       2
             b       1
             c       2
    actress  a       2
             c       3
    dtype: int64
    

    方法一:

    d.groupby([d.type,d.name]).size().groupby(level=[0]).apply(lambda x: x.sort_values(ascending=False))
    
    type     type     name
    actor    actor    c       2
                      a       2
                      b       1
    actress  actress  c       3
                      a       2
    dtype: int64
    

    方法二:

    d1 = d.groupby([d.type,d.name]).size()
    d2 = d1.reset_index()
    d2.columns = ['type', 'actress', 'sz']
    d2.sort_values(by = ['type',  'sz', 'actress'], ascending = [True, False, True])
    
        type    actress sz
    0   actor   a   2
    2   actor   c   2
    1   actor   b   1
    4   actress c   3
    3   actress a   2
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 2015-04-06
      • 2022-01-06
      • 1970-01-01
      • 2017-04-10
      • 2017-01-21
      • 1970-01-01
      • 2016-03-05
      • 2019-01-08
      相关资源
      最近更新 更多