[英]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.在其中,我想编写一个具有以下条件的循环。
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.