代码之家  ›  专栏  ›  技术社区  ›  Garglesoap

数据帧子集上的快速平均

  •  1
  • Garglesoap  · 技术社区  · 6 年前

    我试图循环大量的试验,并计算一些子集的加权平均值。目前,数据格式为长格式,包含试验列、区域分数。

      trial  area       score
    0  T106     0     0.0035435
    1  T106     1     0.0015967
    2  T106     4     0.0003191
    3  T106     4     0.1272919
    4  T288     0     0.1272883
    

    我有大约120000个试验,有4个区域,每个试验可能有10到100个分数,总共约700万行。我的第一个想法是在4个区域的循环中循环所有试验,构建一个临时数据框来计算分数,并将分数添加到外部数据框中:

    for area in range(4):
        for trial in trial_names.iloc[:,0]:  
            Tscore = 0
            temp_trial = pd.DataFrame(trials_long.loc[(trials_long['tname'] == trial) & (trials_long['area'] == int(area))])
            #match score in tria
            temp_trial = temp_trial.merge(scores_df, how='left')
            #sum score for all matching 'trial' +'area'                      #this will be weigted avrg, with >0.5 *2 and >0.9* 3
            temp_trial.loc[temp_trial['score'] > 0.9, ['score']] *= 3        #weight 3x for  >0.9
            temp_trial.loc[temp_trial['score'] > 0.5, ['score']] *= 2        #weight 2x for >0.5
            Tscore = temp_trial['score'].sum() / int(len(temp_trial.index))
            trial_names.loc[trial,area] = Tscore                    #store Tscore somewhere
            Tscore = 0    
    print('done')
    

    0 回复  |  直到 6 年前
        1
  •  5
  •   Quang Hoang    6 年前

    这就是我所尝试的:

    df['weighted'] = df['score']
    df.loc[df['score']>.9, 'weighted'] *= 3        
    df.loc[df['score']>.5, 'weighted'] *= 2
    
    # s is indexed by ('trial', 'area')
    s = df.groupby(['trial', 'area']).weighted.mean()
    

    在6600k上处理700万行需要1.16秒。