我正在研究测量数据集。我在数据框中有数据,如下表所示,数据是每0.5米/秒风速箱的功率和风速。但是我需要根据功率和风速值计算每个箱子的灵敏度列。灵敏度的公式是
sensitivity = abs ( (Pi - Pi_1) / (Vi - Vi_1) )
我们必须从之前的bin值中减去当前bin值的功率和速度。
我需要一个for loop
脚本来实现这个场景。通过使用所有for loop
选项,我真的有点困惑,有人可以帮助我吗?
注意:我从以下DataFrame
脚本中获取了这些值:
uncut = df.groupby(pd.cut(df.normalized_speed, ws_bin))['pt_power_avg', 'normalized_speed'].mean()
数据表:
normalized_speed pt_power_avg [Pi] normalized_speed [Vi] *sensitivity*
[Ci]"
(0, 0.5] 0 0 -
(0.5, 1] 0 0 -
(1, 1.5] 0 0 -
(1.5, 2] 0 0 -
(2, 2.5] 6.46 2.44 2.6
(2.5, 3] 14.22 2.73 26.2
(3, 3.5] 27.05 3.26 24.4
(3.5, 4] 56.67 3.77 58.6
(4, 4.5] 88.55 4.26 64.7
(4.5, 5] 121.95 4.76 66.8
(5, 5.5] 166.87 5.26 89.5
(5.5, 6] 221.16 5.74 112.6
(6, 6.5] 283.94 6.26 122.4
(6.5, 7] 310.32 6.74 54.7
(7, 7.5] 472.59 7.29 297.0
(7.5, 8] 582.02 7.70 261.2
(8, 8.5] 703.98 8.17 261.1
(8.5, 9] 927.60 8.77 375.4
(9, 9.5] 995.10 9.11 194.1
答案 0 :(得分:0)
shift()
代替for looping
在pandas中,您应该使用shift
功能而不是for looping
。 Pandas用于对每行没有for looping
的数据列进行这些精确类型的计算!
假设您的原始数据位于名为DataFrame
的{{1}}中,则该等式将表示为
df
# Calculate equation (broken into numerator and denominator for example)
numerator = df['pt_power_avg [Pi]'] - df['pt_power_avg [Pi]'].shift()
denominator = df['normalized_speed [Vi]'] - df['normalized_speed [Vi]'].shift()
calculated_sensitivity = (numerator / denominator).abs()
# Add to DataFrame
print 'Calculated Sensitivity:'
print calculated_sensitivity
print
答案 1 :(得分:0)
我的结果在[2,2.5]
中没有2.6 normalized_speed
(0, 0.5] NaN
(0.5, 1] NaN
(1, 1.5] NaN
(1.5, 2] NaN
(2, 2.5] NaN
(2.5, 3] 26.180203
(3, 3.5] 24.390952
(3.5, 4] 58.638289
(4, 4.5] 64.677315
(4.5, 5] 66.751720
(5, 5.5] 89.462064
(5.5, 6] 112.621292
(6, 6.5] 122.390346
(6.5, 7] 54.709085
(7, 7.5] 296.962721
(7.5, 8] 261.151143
(8, 8.5] 261.063389
(8.5, 9] 375.387079
(9, 9.5] 194.122176
(9.5, 10] NaN
dtype: float64