每个bin上的python迭代并除以常量

时间:2016-03-24 05:49:40

标签: python pandas

我有一个带有' windspeed bin"的数据框。作为索引,以及' windspeed'," pt_power_avg"和' sigma'的值。

我需要使用公式计算另外2列" fi' &安培; ' PI' 。

fi = ((Vi/Vavg)^2 - (Vi_1/Vavg_1)^2 ) 

Vi_1 =是前一个bin值。

其中Vavg = [0.5, 4,5,6,7,8,9,10,11] - 必须在计算过程中手动添加。

我有以下数据框和分组结果。

pt_power_avg[Pi]    normalized_speed[Vi]    pt_power_sigma    normalized_speed          
(3, 4]  44.779562   3.563218    28.949708
(4, 5]  105.121429  4.507163    27.213929
(5, 6]  195.448421  5.515163    29.652947
(6, 7]  291.834694  6.400732    31.938435
(7, 8]  538.570588  7.537676    30.268088
(8, 9]  727.110345  8.232832    23.327931
(9, 10] 995.100000  9.114639    19.696667

aep= df.groupby(pd.cut(df.normalized_speed,ws_bin))['pt_power_avg','normalized_speed','pt_power_sigma'].mean().fillna(0)

1 个答案:

答案 0 :(得分:0)

使用常规数学运算来计算Vi / Vavg。然后使用DataFrame.diff()计算行之间的差异。这是Jupyter会话的示例:

In [64]: data = """\
         bin\tspeed\tpower          
         (3, 4]\t44.779562\t3.563218
         (4, 5]\t105.121429\t4.507163
         (5, 6]\t195.448421\t5.515163
         (6, 7]\t291.834694\t6.400732
         (7, 8]\t538.570588\t7.537676
         (8, 9]\t727.110345\t8.232832
         (9, 10]\t995.100000\t9.114639
         """
         df = pd.read_table(io.StringIO(data))

         Vavg = pd.Series([0.5, 4,5,6,7,8,9,10,11])
​
In [67]: a = (df.speed/Vavg[:len(V.speed)])**2
​         a

Out[67]: 0     8020.836692
         1      690.657177
         2     1528.003411
         3     2365.763573
         4     5919.556699
         5     8260.772716
         6    12224.987778
         dtype: float64

In [68]: a.diff()
Out[68]: 0            NaN
         1   -7330.179514
         2     837.346234
         3     837.760162
         4    3553.793126
         5    2341.216017
         6    3964.215062
         dtype: float64

In [69]: df['fi'] = a.diff()
         print(df)

                bin       speed  power                     fi
         0   (3, 4]   44.779562         3.563218          NaN
         1   (4, 5]  105.121429         4.507163 -7330.179514
         2   (5, 6]  195.448421         5.515163   837.346234
         3   (6, 7]  291.834694         6.400732   837.760162
         4   (7, 8]  538.570588         7.537676  3553.793126
         5   (8, 9]  727.110345         8.232832  2341.216017
         6  (9, 10]  995.100000         9.114639  3964.215062