pandas 分组并按索引计数排序 [英] Pandas group and sort by index count

查看:141
本文介绍了 pandas 分组并按索引计数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个数据框

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

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

I can group and sort it by any column...

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

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

But what I REALLY want is to sort the programs by their counts

df['Program'].value_counts()

prog1    3
prog3    2
prog2    1
dtype: int64

类似

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

最终的目标是将其绘制成条形按升序或降序排列.我该怎么办?

The ultimate goal is to plot it such that the bars are in ascending or descending order. How can I do that?

推荐答案

您可以将count添加为列,然后可以对其进行排序:

You could just add the count as a column and then you can sort by it:

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

这篇关于 pandas 分组并按索引计数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆