python - 根据最后一行获取新值并检查 ID

标签 python pandas

当前日期范围。

ID  Date     Start Value    Payment
111 1/1/2018    1000        0
111 1/2/2018                100
111 1/3/2018                500
111 1/4/2018                400
111 1/5/2018                0
222 4/1/2018    2000        200
222 4/2/2018                100
222 4/3/2018                700
222 4/4/2018                0
222 4/5/2018                0
222 4/6/2018                1000
222 4/7/2018                0

这是我要获取的数据框。基本上,我试图为每一行填充星号。如您所见,每个 ID 在第一天都有一个起始值。次日起步价=昨日起步价-昨日支付。

   ID   Date    Start Value     Payment
    111 1/1/2018    1000        0
    111 1/2/2018    1000        100
    111 1/3/2018    900         500
    111 1/4/2018    400         400
    111 1/5/2018    0           0
    222 4/1/2018    2000        200
    222 4/2/2018    1800        100
    222 4/3/2018    1700        700
    222 4/4/2018    1000        0
    222 4/5/2018    1000        0
    222 4/6/2018    1000        1000
    222 4/7/2018    0           0

现在,我将 Excel 与此公式结合使用。 起始值=if(本行ID==最后一行ID,最后一行起始值-最后一行付款,起始值)

效果很好,我想知道我是否可以在 Python/Pandas 中完成。谢谢。

最佳答案

我们可以使用groupbyshift + cumsumffill 将为下面的所有行设置初始值相同的Id,那么我们只需要从该行中扣除累计付款直到开始,我们就得到了那一点的剩余值(value)

df.StartValue.fillna(df.groupby('ID').apply(lambda x : x['StartValue'].ffill()-x['Payment'].shift().cumsum()).reset_index(level=0,drop=True))
Out[61]: 
0     1000.0
1     1000.0
2      900.0
3      400.0
4        0.0
5     2000.0
6     1800.0
7     1700.0
8     1000.0
9     1000.0
10    1000.0
11       0.0
Name: StartValue, dtype: float64

通过添加 inplace=Ture 将其重新分配

df.StartValue.fillna(df.groupby('ID').apply(lambda x : x['StartValue'].ffill()-x['Payment'].shift().cumsum()).reset_index(level=0,drop=True),inplace=True)
df
Out[63]: 
     ID      Date  StartValue  Payment
0   111  1/1/2018      1000.0        0
1   111  1/2/2018      1000.0      100
2   111  1/3/2018       900.0      500
3   111  1/4/2018       400.0      400
4   111  1/5/2018         0.0        0
5   222  4/1/2018      2000.0      200
6   222  4/2/2018      1800.0      100
7   222  4/3/2018      1700.0      700
8   222  4/4/2018      1000.0        0
9   222  4/5/2018      1000.0        0
10  222  4/6/2018      1000.0     1000
11  222  4/7/2018         0.0        0

关于python - 根据最后一行获取新值并检查 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50025210/

相关文章:

python - Mechanize - 添加到提交后的表单

python - 将 sklearn 管道 + 嵌套交叉验证放在一起进行 KNN 回归

Python:用.cer文件打开获取公钥然后进行验证

python - Pandas 数据框 : aggregate values within blocks of repeating IDs

pandas - 使 Pandas 在除以零而不是 inf 时提高

python - 压缩序列化 Python 数据最节省空间的方法是什么?

python - 如何将数据附加到 Robot Framework 中的 csv 文件?

python-3.x - Python直方图值错误: range parameter must be finite

python - Pandas 只保留指定的子序列(groupby order 保留子序列)

python - Panda Python - 将一列除以 100(然后四舍五入为 2.dp)