简体   繁体   English

Pandas:如何获取一列中每个项目的最后每日值并从每行中的值中减去它

[英]Pandas: How to get last daily value for each item in one column and subtract it from the value in each row

Consider the following data frame with a timestamp index which may have repeated (ie non-unique) index values, another column that indicates the asset, and another column with the value of that asset at that timestamp.考虑以下具有时间戳索引的数据框,该时间戳索引可能具有重复(即非唯一)索引值、指示资产的另一列和具有该资产在该时间戳处的值的另一列。

df
                     value asset
2021-03-18 11:00:00      4     A
2021-03-18 11:30:00      1     B
2021-03-18 12:00:00      3     A
2021-03-18 12:30:00      2     A
2021-03-18 13:00:00      3     A
2021-03-18 13:30:00      3     A
2021-03-18 14:00:00      1     A
2021-03-18 14:30:00      2     B

For each day, I would like to get the final value of each asset and subtract that from the value in each row, per asset.对于每一天,我想获得每项资产的最终价值,并从每行中的价值中减去每项资产的最终价值。 So in the above table, the last daily value for asset A is 1 (at time 2021-03-18 14:00:00), and for B is 2 (at time 2021-03-18 14:30:00).所以在上表中,资产 A 的最后日值为 1(时间 2021-03-18 14:00:00),而 B 为 2(时间 2021-03-18 14:30:00)。 I would then like to deduct these values from the respective asset value in each row.然后,我想从每行的相应资产价值中扣除这些价值。 So in the first row I want to calculate new_value to equal 4-1 = 3, and for the second row to be 1-2 = -1.所以在第一行我想计算new_value等于 4-1 = 3,第二行是 1-2 = -1。

How can I do that, taking into account that some index values may be repeated since they represent the time at which each asset was traded, and two assets may be traded at the same time.考虑到某些指数值可能会重复,因为它们代表每种资产交易的时间,并且可能同时交易两种资产,我该如何做到这一点。

You can use a groupby/transform with the "last" function:您可以将groupby/transform"last" function 一起使用:

df["new_value"] = df["value"] - df.groupby("asset")["value"].transform("last")

print(df)
                     value asset  new_value
2021-03-18 11:00:00      4     A          3
2021-03-18 11:30:00      1     B         -1
2021-03-18 12:00:00      3     A          2
2021-03-18 12:30:00      2     A          1
2021-03-18 13:00:00      3     A          2
2021-03-18 13:30:00      3     A          2
2021-03-18 14:00:00      1     A          0
2021-03-18 14:30:00      2     B          0

The groupby/transform operation is simply grouping our values by "asset" and getting the "last" element from each group. groupby/transform操作只是按“资产”对我们的值进行分组,并从每个组中获取“最后一个”元素。 Then it maps those values back to their original groups- so every element within that group becomes the "last" element.然后它将这些值映射回它们的原始组 - 因此该组中的每个元素都成为“最后一个”元素。 From there we di simple subtraction between 2 arrays that have the same shape.从那里我们在具有相同形状的 2 个 arrays 之间进行简单的减法。

s = df.groupby("asset")["value"].transform("last")

print(s)

2021-03-18 11:00:00    1
2021-03-18 11:30:00    2
2021-03-18 12:00:00    1
2021-03-18 12:30:00    1
2021-03-18 13:00:00    1
2021-03-18 13:30:00    1
2021-03-18 14:00:00    1
2021-03-18 14:30:00    2
Name: value, dtype: int64

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

相关问题 减去熊猫中每个分组项的唯一值 - Subtract unique value for each groupby item in pandas 从每个客户 ID 的下一列中识别最后一个是值和 0,然后从前一行下一列熊猫中获取值 - Identify the last yes value & 0 from next column for each customer id then get the value from previous row next column pandas 如何获取每行不同列的最后一个有效索引的值 - How to get value of last valid index of a different column for each row 从行中的最后一个非零值中减一; 多列 - Subtract one from last nonzero value in row; multiple column Pandas:对于组中的最后一行,为一列分配一个值 - Pandas: for each last row in a group, assign a column a value Pandas 根据每列的条件获取最后一个值的位置(高效) - Pandas get postion of last value based on condition for each column (efficiently) 从行中的所有值中减去每行的第一个和最后一个值的平均值 - Subtract the average of first and last value of each row from all values in the row 从每一行的最大值中减去最小值,Python Pandas DataFrame - Subtract the minimum value from the maximum value across each row, Python Pandas DataFrame Python数据框获取每行最后一个非空列的值 - Python Dataframe Get Value of Last Non Null Column for Each Row 如何使数据框中的每一行每一列都有一个值? - How to make each row in dataframe have one value for each column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM