【问题标题】:Pandas - access gouped levels in functionPandas - 在函数中访问 gouped 级别
【发布时间】:2015-06-16 18:02:53
【问题描述】:

我有一个数据框,它是 groupby 函数的结果。我现在想根据 groupby 级别执行各种操作:

    d = {'col1': ['foo','bar','foo','bar', 'baz','foo', 'baz', 'bar','bar'],
         'col2': ['a', 'b','c','a','b','c','a','a','b'], 
         'col3': [5,6,6,5,4,6,5,4,3]}

    df = pd.DataFrame(data=d)
    df
        col1    col2    col3
     0  foo     a       5
     1  bar     b       6
     2  foo     c       6
     3  bar     a       5
     4  baz     b       4
     5  foo     c       6
     6  baz     a       5
     7  bar     a       4
     8  bar     b       3

    df2 = df.groupby(['col1','col2']).count()
    df2['col4'] = ''
    df2
                      col3  col4
          col1  col2    
          bar   a     2    
                b     2    
          baz   a     1    
                b     1    
          foo   a     1     
                c     2    

现在我想写一个函数,它会给我 col4 中 col3 的平均值。请注意,我的实际 df2 对 col1 和 col2 有许多不同的 val(即,我需要访问 [foo,bar,baz] 分组)。我一直在尝试各种不同的子索引方式,但没有成功。

【问题讨论】:

  • col3 是什么意思?如果这是你想要的,mean 需要一个 level 参数

标签: python pandas


【解决方案1】:

正如 Ed 所说,您需要提供关卡:

>>> df2.mean(level=0)
      col3
col1      
bar    2.0
baz    1.0
foo    1.5

>>> df2.mean(level=1)
          col3
col2          
a     1.333333
b     1.500000
c     2.000000

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 2022-01-06
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2019-05-22
    • 2016-11-07
    • 2015-07-29
    相关资源
    最近更新 更多