简体   繁体   English

从变量中逐列累积减去 pandas 组

[英]Cumulative subtracting a pandas group by column from a variable

Hi I have a dataframe that lists items that I own, along with their Selling Price.嗨,我有一个 dataframe 列出了我拥有的物品及其售价。

I also have a variable that defines my current debt.我还有一个变量来定义我当前的债务。 Example:例子:

import pandas as pd

current_debt = 16000

d = { 
    'Person' : ['John','John','John','John','John'],
    'Ïtem': ['Car','Bike','Computer','Phone','TV'],
    'Price':[10500,3300,2100,1100,800],
    }
           
df = pd.DataFrame(data=d)
df

在此处输入图像描述

I would like to "payback" the current_debt starting with the most expensive item and continuing until the debt is paid.我想从最昂贵的项目开始“偿还” current_debt并一直持续到偿还债务。 I would like to list the left over money aligned to the last item sold.我想列出与最后售出的物品对齐的剩余资金。 I'm hoping the function can inlcude a groupby clause for Person as sometimes there is more than one name in the list我希望 function 可以为Person包含一个groupby子句,因为有时列表中有多个名称

My expected output for the debt in the example above would be:我对上述示例中债务的预期 output 将是:

在此处输入图像描述

If anyone could help with a function to calculate this that would be fantastic.如果有人可以帮助使用 function 来计算这个,那就太棒了。 I wasnt sure whether I needed to convert the dataframe to a list or it could be kept as a dataframe.我不确定是否需要将 dataframe 转换为列表,或者它可以保留为 dataframe。 Thanks very much!非常感谢!

Using a cumsum transformation and np.where to cover your logic for the final price column:使用 cumsum 转换和np.where来涵盖最终价格列的逻辑:

import numpy as np

df = df.sort_values(["Person", "Price"], ascending=False)
df['CumPrice'] = df.groupby("Person")['Price'].transform('cumsum')
df['Diff'] = df['CumPrice'] - current_debt
df['PriceLeft'] = np.where(
    df['Diff'] <= 0, 
    0, 
    np.where(
         df['Diff'] < df['Price'], 
         df['Diff'],
         df['Price']
    )
)

Result:结果:

  Person      Item  Price  CumPrice  Diff  PriceLeft
0   John       Car  10500     10500 -5500          0
1   John      Bike   3300     13800 -2200          0
2   John  Computer   2100     15900  -100          0
3   John     Phone   1100     17000  1000       1000
4   John        TV    800     17800  1800        800

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM