Edit: добавлен np.abs, чтобы убедиться, что вы берете абсолютную величину разности.
Вы можете использовать pandas diff
для этого, а затем np.where
для условия:
import numpy as np
import pandas as pd
data = {
'Temperature': [25,25,25,25,25,25,25,25,25,25,105,105,105,105,105,105,105,105,105,105],
'Voltage': [3.3,3.3,3.3,3.3,3.3,3.45,3.45,3.45,3.45,3.45,3.3,3.3,3.3,3.3,3.3,3.45,3.45,3.45,3.45,3.45],
'Data': [2,2.5,3.7,3.5,2.7,1.9,1.7,1.5,2,2.9,3,3.5,4.7,4.5,3.7,2.5,2.3,2.1,3.3,4]
}
df = pd.DataFrame(data)
df['difference'] = df['Data'].diff(1)
df['flag'] = np.where(np.abs(df['difference']) >= 1,'More than 1','Less than one')
print(df)
Выход:
Temperature Voltage Data difference flag
0 25 3.30 2.0 NaN Less than one
1 25 3.30 2.5 0.5 Less than one
2 25 3.30 3.7 1.2 More than 1
3 25 3.30 3.5 -0.2 Less than one
4 25 3.30 2.7 -0.8 Less than one
5 25 3.45 1.9 -0.8 Less than one
6 25 3.45 1.7 -0.2 Less than one
7 25 3.45 1.5 -0.2 Less than one
8 25 3.45 2.0 0.5 Less than one
9 25 3.45 2.9 0.9 Less than one
10 105 3.30 3.0 0.1 Less than one
11 105 3.30 3.5 0.5 Less than one
12 105 3.30 4.7 1.2 More than 1
13 105 3.30 4.5 -0.2 Less than one
14 105 3.30 3.7 -0.8 Less than one
15 105 3.45 2.5 -1.2 More than 1
16 105 3.45 2.3 -0.2 Less than one
17 105 3.45 2.1 -0.2 Less than one
18 105 3.45 3.3 1.2 More than 1
19 105 3.45 4.0 0.7 Less than one