对datafram中的每个容器进行python迭代

2024-03-29 15:13:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个测量数据集。我在下表中的数据框中有数据,数据是每0.5m/s风速箱的功率和风速。但我需要根据功率和风速值计算每个箱子的灵敏度列。灵敏度公式为

sensitivity = abs ( (Pi - Pi_1) / (Vi - Vi_1) )

我们必须从之前的bin值中减去当前的功率和速度bin值。

对于这个场景,我需要一个for loop脚本。我真的有点困惑使用所有的for loop选项,有人能帮我吗?

注意:我从下面的DataFrame脚本中获得这些值:

^{pr2}$

数据表:

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    

Tags: 数据脚本loopforbinpi功率公式
2条回答

没有2.6英寸[2,2.5]我的成绩低于

       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

使用shift()代替for looping

在pandas中,应该使用^{}特性而不是for looping。Pandas被构建成对数据列执行这些精确类型的计算,而不必对每一行执行for looping!在

假设您的原始数据在一个名为dfDataFrame中,那么方程将表示为

# 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

^{pr2}$

相关问题 更多 >