【问题标题】:Pandas group and sort by index countPandas 分组并按索引计数排序
【发布时间】:2015-12-31 01:16:19
【问题描述】:

假设我有这个数据框

d = {     'Path'   : ['abc', 'abc', 'ghi','ghi', 'jkl','jkl'],
          'Detail' : ['foo', 'bar', 'bar','foo','foo','foo'],
          'Program': ['prog1','prog1','prog1','prog2','prog3','prog3'],
          'Value'  : [30, 20, 10, 40, 40, 50],
          'Field'  : [50, 70, 10, 20, 30, 30] }


df = DataFrame(d)
df.set_index(['Path', 'Detail'], inplace=True)
df
               Field Program  Value
Path Detail                      
abc  foo        50   prog1     30
     bar        70   prog1     20
ghi  bar        10   prog1     10
     foo        20   prog2     40
jkl  foo        30   prog3     40
     foo        30   prog3     50

我可以按任何列对其进行分组和排序...

df_count = df.groupby('Program')
df_count.apply(lambda x: x.sort())

Program Path    Detail  Field   Program Value
prog1   abc     foo       50    prog1   30
        ghi     foo       20    prog1   40
        jkl     bar       10    prog1   30
prog2   abc     bar       70    prog2   20
prog3   ghi     foo       10    prog3   60
        jkl     foo       30    prog3   50

但我真正想要的是按程序的计数对程序进行排序

df['Program'].value_counts()

prog1    3
prog3    2
prog2    1
dtype: int64

有点像

df_count.apply(lambda x: x.sort('Programs'.value_counts()))

最终目标是绘制它,使条形按升序或降序排列。我该怎么做?

【问题讨论】:

    标签: python sorting pandas count grouping


    【解决方案1】:

    您可以将count 添加为一列,然后按它进行排序:

    In [20]:
    df['count'] = df['Program'].map(df['Program'].value_counts())
    df
    
    Out[20]:
                 Field Program  Value  count
    Path Detail                             
    abc  foo        50   prog1     30      3
         bar        70   prog1     20      3
    ghi  bar        10   prog1     10      3
         foo        20   prog2     40      1
    jkl  foo        30   prog3     40      2
         foo        30   prog3     50      2
    
    In [23]:
    df.sort('count', ascending=False)
    
    Out[23]:
                 Field Program  Value  count
    Path Detail                             
    abc  foo        50   prog1     30      3
         bar        70   prog1     20      3
    ghi  bar        10   prog1     10      3
    jkl  foo        30   prog3     40      2
         foo        30   prog3     50      2
    ghi  foo        20   prog2     40      1
    

    【讨论】:

    • 谢谢!我知道这是一种选择,但我希望有一种更优雅的方式来做到这一点,而无需添加额外的列。
    • 除非您创建了一个虚拟系列/df 并使用该索引重新索引您的 df,否则您实际上无法按根本不存在的东西进行排序
    猜你喜欢
    • 2021-12-16
    • 2020-04-30
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    相关资源
    最近更新 更多