1

使用 R 中的 doBy 包,我们对组进行汇总,并得到与原始数据相同形状和顺序的结果:

> require(doBy)
> df <- data.frame(
          first = c('bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'),
          second = c('one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'), 
          data = c(-0.424972, 0.567020, 0.276232, -1.087401, -0.673690, 0.113648, -1.478427, 0.524988))
> df
  first second      data
1   bar    one -0.424972
2   bar    two  0.567020*emphasized text*
3   baz    one  0.276232
4   baz    two -1.087401
5   foo    one -0.673690
6   foo    two  0.113648
7   qux    one -1.478427
8   qux    two  0.524988
> df['data.sum'] = summaryBy(data~first, data=df, FUN=sum, full.dimension=T)['data.sum']
> df
  first second      data  data.sum
1   bar    one -0.424972  0.142048
2   bar    two  0.567020  0.142048
3   baz    one  0.276232 -0.811169
4   baz    two -1.087401 -0.811169
5   foo    one -0.673690 -0.560042
6   foo    two  0.113648 -0.560042
7   qux    one -1.478427 -0.953439
8   qux    two  0.524988 -0.953439

DataFrame当按多个索引之一分组时,有没有办法在熊猫中做同样的事情?

>>> from pandas import DataFrame
>>> df = DataFrame({ 
                 'first': ['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
                 'second': ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
                 'data': [-0.424972, 0.567020, 0.276232, -1.087401, -0.673690, 0.113648, -1.478427, 0.524988] })
>>> df = df.set_index(['first', 'second'])
>>> s = df.groupby(level='first')['data'].sum()
>>> df.join(s, on='first', rsuffix='.sum')

KeyError: 'no item named first'
4

2 回答 2

2

怎么样:

df['data.sum'] = df.groupby('first')['data'].transform(np.sum)

您还可以传递as_index=False给 groupby 以在聚合(或调用reset_index结果)时获得更多类似 R 的行为

于 2012-04-07T20:24:34.690 回答
0

怎么样

from pandas import *
df = DataFrame({ 
    'first': ['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
    'second': ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
    'data': [-0.424972, 0.567020, 0.276232, -1.087401, -0.673690, 0.113648, -1.478427, 0.524988] 
})

df2 = df.join(df.groupby("first").sum().rename(columns={"data":"sum_data"}), 
              on="first")
于 2012-04-04T01:15:43.610 回答