[英]Add the previous column value with the current value
I am trying to figure out the logic to write the python code for below requirement.我试图找出为以下要求编写 python 代码的逻辑。 Basically what I want the expected output is, if IDLE != 0 , 'Duration' value should be above cell value + idle.基本上我想要预期的 output 是,如果IDLE != 0 ,“持续时间”值应该高于单元值 + 空闲。 I will be getting a live data with data resolution of 5 minutes.我将获得数据分辨率为 5 分钟的实时数据。 I want to send the duration to a collection.我想将持续时间发送到集合。 How can I create a logic for this?我怎样才能为此创建一个逻辑?
Consider below logic (not syntax) I came up until now:考虑一下我到现在为止的逻辑(不是语法):
if (df["SPEED"].mean()) != 0:
df["DURATION"] = 0
for i in range (len(df)):
if (df.loc[i, "IDLE"]) == 0:
df["DURATION"] = df["DISTANCE"]/df["SPEED"]
for i in range (len(df)):
if (df.loc[i, "DURATION"]) == 0:
(df.loc[i, "DURATION"]) = ((df.loc[i-1, "DURATION"])+(df.loc[i, "IDLE"]))
.
.
The above code I written is not working.我写的上面的代码不起作用。 if wondering why if mean(speed)!=0 ?如果想知道为什么if mean(speed)!=0 ? so that initially when speed = 0, I don't want to send output How should I proceed next?所以最初当速度= 0时,我不想发送 output 我接下来应该如何进行?
DISTANCE SPEED IDLE DURATION EXPECTED_OUTPUT
0 X 0.0 5 0 0
1 X 0.0 5 0 0
2 X 0.2 0 10 10
3 X 0.2 0 10 10
4 X 0.2 0 10 10
5 X 0.2 0 10 10
6 X 0.0 5 0 15
7 X 0.0 5 0 20
8 X 0.2 0 10 10
9 X 0.2 0 10 10
10 X 0.2 0 10 10
11 X 0.2 0 10 10
i worked out a solution我想出了一个解决方案
for i in range (1,len(df)):
if (df.loc[i, "DURATION"]) == 0:
(df.loc[i, "DURATION"]) = ((df.loc[i-1, "DURATION"])+(df.loc[i, "IDLE"]))
One solution might be to iterate over the dataframe.一种解决方案可能是迭代 dataframe。
speed
other than 0. There are a lot of ways to do that, you can try the following:找到第一个speed
不是0的索引。有很多方法可以做到这一点,你可以尝试以下:df[df["SPEED"] != 0].index[0]
Duration
column in the EXPECTED_OUTPUT
column:复制EXPECTED_OUTPUT
列中的Duration
列:df["EXPECTED_OUTPUT"] = df["DURATION"]
Iterate over the dataframe
rows starting at the index find in step 1.(here I make the assumption that index rows are sorted).从步骤 1 中的索引 find 开始迭代dataframe
行。(这里我假设索引行已排序)。
duration
is 0:检查duration
是否为 0:If 1 is verified, verified a previous row exists:如果 1 被验证,则验证前一行存在:
-> YES: add the previous EXPECTED_OUTPUT
cell to current IDLE
-> YES:将前一个EXPECTED_OUTPUT
单元添加到当前IDLE
-> NO: set the value you want (here NaN). -> NO:设置你想要的值(这里是 NaN)。 Set nothing to let the DURATION
value什么都不设置让DURATION
值
Full code :完整代码:
# Find index where speed starting being != 0
index = df[df["SPEED"] != 0].index[0]
print(index)
# 2
# Copy duration column in expected output
df["EXPECTED_OUTPUT"] = df["DURATION"]
# Iterate over duration row
for i in range(index, len(df)):
# If duration == 0
if df.loc[i, 'DURATION'] == 0:
# Check a previous row exist
if i > 0:
df.loc[i, 'EXPECTED_OUTPUT'] = df.loc[i, "IDLE"] + df.loc[i - 1, "EXPECTED_OUTPUT"]
# If previous row doesn't exist : set NaN
# You can remove this else statement to have the "duration" value
# Depends on you
else:
df.loc[i, 'EXPECTED_OUTPUT'] = np.NaN
print(df)
# DISTANCE SPEED IDLE DURATION EXPECTED_OUTPUT
# 0 X 0.0 5 0 0
# 1 X 0.0 5 0 0
# 2 X 0.2 0 10 10
# 3 X 0.2 0 10 10
# 4 X 0.2 0 10 10
# 5 X 0.2 0 10 10
# 6 X 0.0 5 0 15
# 7 X 0.0 5 0 20
# 8 X 0.2 0 10 10
# 9 X 0.2 0 10 10
# 10 X 0.2 0 10 10
# 11 X 0.2 0 10 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.