简体   繁体   English

根据“ID”列返回上一行值以查找当前行与上一行之间的差异

[英]Return previous row value based on "ID" column to find differences between current row and previous row

I am new to python.我是 python 的新手。 I am having a problem trying to look up previous row values based on "ID" and "timestamp" in a python dataframe.我在尝试根据 python dataframe 中的“ID”和“时间戳”查找前一行值时遇到问题。 Can anyone please advise how I should derive the solution?谁能告诉我应该如何得出解决方案?

Below is a simple example:下面是一个简单的例子:

For example:例如:

id: timestamp: sapStock laststockdate laststockvalue id: 时间戳: sapStock laststockdate laststockvalue

167777 14/12/2021 184 13/12/2021 143 169406 14/12/2021 56 13/12/2021 60 167777 14/12/2021 184 13/12/2021 143 169406 14/12/2021 56 13/12/2021 60

在此处输入图像描述

#Import Libraries
import pandas as pd

#Read CSV
df = pd.read_csv(r'C:\Users\User\OneDrive\Desktop\Test.csv')

#Preview dataframe
df

#Get last value of sapStock based on "id" and "timestamp"
df['sapStockDifference'] = df.sapStock.diff(periods=1)
df

在此处输入图像描述

I hope this may help:我希望这可能会有所帮助:

import pandas as pd
import numpy as np

df = pd.DataFrame({'id': ['169653', '167777', '169406', '165253', '169653', '167777', '169406', '165253', '169653', '167777', '169406', '165253', '169653', '167777', '169406', '165253'],
                   'sapStock': [234, 162, 36, 47, 264, 158, 60, 46, 279, 143, 60, 46, 364, 184, 56, 46],
                   'timestamp': ['11/12/2021', '11/12/2021', '11/12/2021', '11/12/2021', '12/12/2021', '12/12/2021', '12/12/2021', '12/12/2021', '13/12/2021', '13/12/2021', '13/12/2021', '13/12/2021', '14/12/2021', '14/12/2021', '14/12/2021', '14/12/2021']})
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['laststockvalue'] = df.sort_values(['timestamp']).groupby(df.id)['sapStock'].apply(lambda x: x.shift(1)).fillna(value=np.nan)
df['Difference'] = df.sort_values(['timestamp']).groupby(df.id)['sapStock'].apply(lambda x: x - x.shift(1)).fillna(value=np.nan)
print(df)

#         id  sapStock  timestamp  laststockvalue  Difference
# 0   169653       234 2021-11-12             NaN         NaN
# 1   167777       162 2021-11-12             NaN         NaN
# 2   169406        36 2021-11-12             NaN         NaN
# 3   165253        47 2021-11-12             NaN         NaN
# 4   169653       264 2021-12-12           234.0        30.0
# 5   167777       158 2021-12-12           162.0        -4.0
# 6   169406        60 2021-12-12            36.0        24.0
# 7   165253        46 2021-12-12            47.0        -1.0
# 8   169653       279 2021-12-13           264.0        15.0
# 9   167777       143 2021-12-13           158.0       -15.0
# 10  169406        60 2021-12-13            60.0         0.0
# 11  165253        46 2021-12-13            46.0         0.0
# 12  169653       364 2021-12-14           279.0        85.0
# 13  167777       184 2021-12-14           143.0        41.0
# 14  169406        56 2021-12-14            60.0        -4.0
# 15  165253        46 2021-12-14            46.0         0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 根据此行另一列与上一行的差确定一个值 - Determine a value based on the difference between another column of this row and the previous row 将当前行和上一行的差异附加到新列,用于多列 - Appending to a new column the differences of current row and previous row, for multiple columns 在列值上查找具有相同 ID 和条件的上一行以分配权重 - Find previous row with the same ID and condition on column value to assign weight 用 Pandas 中的前一行列填充当前行和列值 - Fill current row and column value with previous row column in Pandas 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 根据条件创建一个 Boolean 列,该列涉及 Pandas 中当前行和上一行的列的值 - Create a Boolean column based on condition which involves value from a column of the current row and previous row in Pandas 根据条件从当前行和上一行中的值创建列表 - Create list from value in current row and previous row based on condition 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row 将当前行值与前一行值进行比较 - Compare current row value to previous row values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM