1

我有一个dataframe物种调查计数,需要按多个标准汇总行。主要问题是我需要匹配不同年份的季节性样本。例如,2005 年春季的样本将与 2006 年秋季的样本相匹配,其中现场样本方法和重复相匹配。下面是一个简单的数据示例:

# create the factors and dataframe
a = repeat('AAA',4)
b = repeat('BBB',2)
y1 = np.array([2005, 2006])
y2 = np.array([2005, 2007])
r = np.array([1, 1, 2, 2, 1 , 1])
d = {'site' : hstack((a,b,a,b,a,b,a,b)),
     'year' : hstack((y1, y1, y1, y2, y2, y2, y1, y1, y1, y2, y2, y2)),
     'season' : hstack((repeat('AUTUMN', 6), repeat('SPRING', 6), repeat('AUTUMN', 6), repeat('SPRING', 6))),
     'method' : hstack((repeat('EDGE', 12), repeat('RIFFLE', 12))),
     'replicate' : hstack((r, r, r, r))}
df = DataFrame(d)

# now add some species
df['sp1'] = 1
df['sp2'] = 2
df['sp3'] = 3

数据框中的每一行都是一个样本。目前,我正在创建一个新的合并“id”列,遍历每个“SPRING”样本以搜索匹配的秋季样本,并在对“id”进行分组之前更新这两个样本的“id”。例如:

df['id'] = 'na' # new column for combined season id
grouped = df.groupby('season') # split table by season 

for name, group in grouped:
    if name == 'AUTUMN':
        aut = group #autumn lookup list
    if name == 'SPRING':
        # for each spring sample
        for row_index, row in group.iterrows():
            # check for matching autumn sample
            n = aut[
                (aut['site'] == row['site']) &
                (aut['year'] == row['year'] + 1) &
                (aut['method'] == row['method']) &
                (aut['replicate'] == row['replicate'])].index
            if n:
                # create new combined season id
                new_id = row['site'] + \
                         str(row['year'])[-2:] + \
                         str(row['year'] + 1)[-2:] + \
                         row['method'][:1] + \
                         str(row['replicate'])
                # update id spring sample with matching autumn 
                df.id.ix[row_index] = new_id
                # get matching autumn table index
                df.id.ix[n] = new_id
df = df[df['id'] != 'na']
combined = df.groupby(['method', 'id', 'site']).sum()
combined = combined.drop(['year', 'replicate'], axis=1)

这种方法效果很好,但我认为它有点笨拙,而且根本不是通用的。有没有一种矢量化的方式来以这种方式聚合数据?很抱歉帖子的长度,如果有任何不清楚的地方,请告诉我。

提前致谢

4

1 回答 1

1

编辑:更正的代码

这个怎么样:

adjyear = np.where(df.season == 'SPRING', df.year + 1, df.year)
adjyear.name = 'year'

grouped = df.groupby(['method', 'replicate', 'site', adjyear])
grouped = grouped['sp1', 'sp2', 'sp3']    

grouped.sum()[grouped.size() > 1]

这给了我:

In [20]: grouped.sum()[grouped.size() > 1]
Out[20]: 
                            sp1  sp2  sp3
method replicate site year               
EDGE   1         AAA  2006    2    4    6
                 BBB  2006    2    4    6
       2         AAA  2006    2    4    6
RIFFLE 1         AAA  2006    2    4    6
                 BBB  2006    2    4    6
       2         AAA  2006    2    4    6
于 2012-07-11T15:04:44.787 回答