简体   繁体   English

将上一列值与当前值相加

[英]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。

  1. Find the index of the first 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]
  1. Copy the Duration column in the EXPECTED_OUTPUT column:复制EXPECTED_OUTPUT列中的Duration列:
df["EXPECTED_OUTPUT"] = df["DURATION"]
  1. 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行。(这里我假设索引行已排序)。

    1. Check if the duration is 0:检查duration是否为 0:
    2. 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.

相关问题 Pandas - 添加一列,其值根据当前行和前一行中的另一列值计算得出 - Pandas - add a column with value computed based on another column value in current and previous row Python将先前的dict值添加到当前值 - Python add previous dict value to current one 添加到列表中,当前行某列的值a DataFrame 仅当前面的行通过测试 - Add to the list, a value of a column of the current row of a DataFrame only if the previous rows pass the test 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 用 Pandas 中的前一行列填充当前行和列值 - Fill current row and column value with previous row column in Pandas 当当前行和先前行中的名称(A 列中)匹配时,如何创建具有先前值(B 列中)的列? - How do I create a column with a previous value (in column B) when the names (in column A) in current and previous rows matches? 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row Python pandas 加 N 乘以当前秒的值,再加上 N 乘以前一秒的值 - Python pandas add N multiplied by the value of the current second and add with the value of N multiplied by the value of the previous second 如果列等于 Pandas 中的值,如何使当前行值等于前一行值? - How to make current row value equal to previous row value if a column equals a value in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM