简体   繁体   English

分组/识别累积和(在 pandas 数据帧中循环)

[英]Grouping/identifying cumulative sum (Looping in pandas dataframe)

I have a data frame like below:我有一个如下数据框:

df = pd.DataFrame({'Letter': ['C','B','A','D','E','H','G'],
                   'Number': [5,5,5,7,7,10,10],
                   'Value of Letter': [10,15,15,25,20,30,25],
                   'Amount': [100,'','',30,'',54,''],
                   'Realisation Index': [1,3,5,2,3,4,5]
                  })

In it, I want to write a loop with the following conditions.在其中,我想编写一个具有以下条件的循环。

  1. For each number in the number, column pandas should deduct the "Value of Letter" from the "Amount" column.对于数字中的每个数字,pandas 栏应从“金额”栏中扣除“字母值”。 Conditions are:条件是:
  2. When deducting the value of the letter, first pandas should prioritize based on Realization index (SAY if the realization index is 1 then the relevant amount in "Value of letter" column should be deducted first and so on until all security values are deducted)扣除字母价值时,首先pandas应根据变现指数优先(假设变现指数为1则应先扣除“字母价值”栏中的相关金额,以此类推,直到扣除所有证券价值)
  3. The highest value in "Value of Letter" should be deducted first from the amount.应先从金额中扣除“信件价值”中的最高值。

I am trying to write a loop using the above conditions in python/pandas and trying to compute the "Amount 2" column.我正在尝试在 python/pandas 中使用上述条件编写一个循环并尝试计算“Amount 2”列。

The expected output is as follows.预期的output如下。

df = pd.DataFrame({'Letter': ['C','B','A','D','E','H','G'],
                   'Number': [5,5,5,7,7,10,10],
                   'Value of Letter': [10,15,15,25,20,30,25],
                   'Amount': [100,'','',30,'',54,''],
                   'Realisation Index': [1,3,5,2,3,4,5],
                   'Amount 2':[90,75,60,5,-15,24,-1]
                  })

Let do it动手吧

df.Amount=pd.to_numeric(df.Amount,errors='coerce')
df['New']=df.sort_values('Realisation Index').groupby('Number').apply(lambda x : max(x['Amount'])-x['Value of Letter'].cumsum()).reset_index(level=0,drop=True)
df
  Letter  Number  Value of Letter  Amount  Realisation Index   New
0      C       5               10   100.0                  1  90.0
1      B       5               15     NaN                  3  75.0
2      A       5               15     NaN                  5  60.0
3      D       7               25    30.0                  2   5.0
4      E       7               20     NaN                  3 -15.0
5      H      10               30    54.0                  4  24.0
6      G      10               25     NaN                  5  -1.0

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

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