0

我正在从数据帧df中循环打开和关闭的传感器读取温度数据。每次传感器打开时,大约需要 5 行数据来进行热平衡。我想在温度列上运行的任何统计数据中忽略传感器预热时间降低的温度值,并且在绘图时也忽略它们。数据框中的三列是“”、“传感器状态”和“温度”。我创建了一个名为“ Sensor_Warmup_State ”的第四列,它是使用循环创建的,并在“ Sensor_State ”中检测到0后将所有值变为0' 列在接下来的 5 个单元格中。然后我将“温度”乘以“ Sensor_Warmup_State ”得到“ Processed_Temp ”。这行得通,但我知道应该有一个更pythonic、更快的方法来做到这一点,我只是还没有专业知识。

这就是我所拥有的。创建数据框:

import numpy as np
a=np.arange(1,21).tolist()
b = (np.zeros((2), dtype=int)).tolist()
c = (np.ones((18), dtype = int)).tolist()
d = b + c
e = [0,0,1,2,4,8,9,10,10,10,10,10,10,10,10,10,10,10,10,10]
data = {'Seconds': a, 'Sensor_State': d, 'Temperature': e}
df = pd.DataFrame.from_dict(data)
df['Sensor_Warmup_State'] = 0
df

df输出截图

要创建最后两列:

NumOfRows = df['Sensor_State'].size
x=0
for index, value in df['Sensor_State'].iteritems():
    if (value == 0) & (index < NumOfRows-5):
        df['Sensor_Warmup_State'].iloc[index] = 0
    elif (value == 1) & (index < NumOfRows-5):
        df.loc[(index + 5), 'Sensor_Warmup_State'] = 1
df['Processed_Temp'] = df['Sensor_Warmup_State'] * df['Temperature']
df

带有新列的 df 输出的屏幕截图

4

1 回答 1

0

在这里,我想出了一个更好的方法,使用 .shift(),这比最初概述的循环要简单得多,而且速度快 30%。我编辑了起始数据框以说明 Sensor_State 何时从 0 变为 1、回到 0 和 1 以说明这些情况。希望这可以帮助某人:

In [1]:
import numpy as np
import pandas as pd

a=np.arange(1,24).tolist()
b=[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
c = [0,0,1,2,4,8,9,10,10,10,0,0,0,0,0,2,4,8,10,10,10,10,10]
data = {'Seconds': a, 'Sensor_State': b, 'Temperature': c}
df = pd.DataFrame.from_dict(data)
df['Sensor_Warmup_State'] = 0
df

Out[1]: 
    Seconds  Sensor_State  Temperature  Sensor_Warmup_State
0         1             0            0                    0
1         2             0            0                    0
2         3             1            1                    0
3         4             1            2                    0
4         5             1            4                    0
5         6             1            8                    0
6         7             1            9                    0
7         8             1           10                    0
8         9             1           10                    0
9        10             1           10                    0
10       11             0            0                    0
11       12             0            0                    0
12       13             0            0                    0
13       14             0            0                    0
14       15             0            0                    0
15       16             1            2                    0
16       17             1            4                    0
17       18             1            8                    0
18       19             1           10                    0
19       20             1           10                    0
20       21             1           10                    0
21       22             1           10                    0
22       23             1           10                    0

新代码:

In [2]:
df['Sensor_Warmup_State'] = (df['Sensor_State'] == 1) &\
(df['Sensor_State'].shift(1) == 1) &\
(df['Sensor_State'].shift(2) == 1) &\
(df['Sensor_State'].shift(3) == 1) &\
(df['Sensor_State'].shift(4) == 1) &\
(df['Sensor_State'].shift(5) == 1)

df['Processed_Temp'] = df['Sensor_Warmup_State'] * df['Temperature']
df

Out[2]: 
    Seconds  Sensor_State  Temperature  Sensor_Warmup_State  Processed_Temp
0         1             0            0                False               0
1         2             0            0                False               0
2         3             1            1                False               0
3         4             1            2                False               0
4         5             1            4                False               0
5         6             1            8                False               0
6         7             1            9                False               0
7         8             1           10                 True              10
8         9             1           10                 True              10
9        10             1           10                 True              10
10       11             0            0                False               0
11       12             0            0                False               0
12       13             0            0                False               0
13       14             0            0                False               0
14       15             0            0                False               0
15       16             1            2                False               0
16       17             1            4                False               0
17       18             1            8                False               0
18       19             1           10                False               0
19       20             1           10                False               0
20       21             1           10                 True              10
21       22             1           10                 True              10
22       23             1           10                 True              10
于 2020-05-27T21:34:14.717 回答