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

在两个数据帧列之间执行计算的最快方法?

  •  1
  • pookie  · 技术社区  · 8 年前

    我有一个有600万行的熊猫数据框。列包括:

    ['x', 'y']
    

    我需要应用一个简单的计算 x y ,并将其附加到数据帧。

    这就是我所尝试的:

    '''
    Calculates the height of a pressure level in feet
    '''
    def pressure_to_elevation(P, T = None):
    
        sea_level_pressure = 1013.25
    
        if T is not None:
            # https://www.omnicalculator.com/physics/air-pressure-at-altitude
    
            P0 = sea_level_pressure
            g = 9.80665
            M = 0.0289644
            R0 = 8.31447
    
            m = (np.log(P/P0)*T) / -(g*M/R0)
            f = 3.28084 * m
            return f
    
        b = 0.190284
        c = 145366.45
    
        return (1-math.pow((P/sea_level_pressure), b)) * c
    
    
    test_df['result'] = test_fd.apply(lambda row: pressure_to_elevation(row['x'], row['y']),axis=1)
    

    不幸的是,这需要很长时间。。。事实上,我还没有看到它完成。

    有没有更快的方法?

    2 回复  |  直到 8 年前
        1
  •  2
  •   MaxU - stand with Ukraine    8 年前

    试试这个:

    def pressure_to_elevation(P, T):
    
        sea_level_pressure = 1013.25
    
        P0 = sea_level_pressure
        g = 9.80665
        M = 0.0289644
        R0 = 8.31447
    
        b = 0.190284
        c = 145366.45
    
        return np.where(T.notnull(),
                        3.28084 * ((np.log(P/P0)*T) / -(g*M/R0)),
                        (1-np.pow((P/sea_level_pressure), b)) * c)
    

    用法:

    test_df['result'] = pressure_to_elevation(test_df['x'], test_df['y'])
    
        2
  •  0
  •   cstainbrook    8 年前

    我相信,如果您将其分解为单独的步骤,并避免遍历整个数据帧,速度将显著提高。试一试以下内容。

    test_df['result_1'] = (test_df['x']/sea_level_pressure)
    test_df['result_1'] = test_df['result']**0.190284
    test_df['result_1'] = (1 - test_df['result'])*145366.45
    
    test_df['result_2'] = 3.28084*((np.log(test_df['x']/sea_level_pressure)*test_df['y'])/(-1*(9.80665*0.0289644/8.31447)))
    
    test_df['final_result'] = np.where(pd.isnull(test_df['y']), test_df['result_1'], test_df['result_2'])