Here is the dataset
import pandas as pd
d = {'Key':['A','A','A','A'],'Rank':[1,2,3,4],'col1': [15000,12000,6000,7000], 'col2': [15000,10000,0,0],'col4': [10000,10000,10000,10000],'col5': [0,0,0,0] }
df = pd.DataFrame(data=d)
df
- Col1= Max values it can take
- Col2=Current value it holds
- Col4:Remaining value that should fit in any of these records.
I am trying to fill in the 'col5' with possible max value that it can take.Where 'Col1' defines its maximum limit and 'col2' shows its current value. If it fits max value then move to the next row. The value that it can fit is determined by 'col4'. Please see below example.
Example:
- first record with rank 1 Col1=15000 and col2=15000 then move to next row.
- second record with rank2 col1=12000 and col2=10000. Here we can see that its max is 12000 so I can add 2000 more, also need to make sure col5>2000 so col5=2000 and col4 for next record will be 10000-2000=8000
Here is the end dataset which should look like
Below is the code which I have tried
for index, row in df.iterrows():
#print(row['col1'], row['col2'])
if row['col1']>row['col2']:
if (row['col1']-row['col2'])<row['col2']:
row['col5']=row['col1']-row['col2']
else:
row['col5']=row['col2']
#return
print(row['col1'], row['col2'],row['col5'])