I have the following pandas DataFrame:
df = pd.DataFrame([[1,100, 200, float('nan')],
[1, 110, float('nan'), float('nan')],
[1, 115, float('nan'), float('nan')],
[2, 50, 100, float('nan')],
[2, 50, float('nan'), float('nan')],
[3, 100, 100, float('nan')],
[3, 125, float('nan'), float('nan')]],
columns=['User', 'Amount', 'FAB', 'Result'])
╔══════╦════════╦══════╦════════╗
║ User ║ Amount ║ FAB ║ Result ║
╠══════╬════════╬══════╬════════╣
║ 1 ║ 100 ║ 200 ║ NaN ║
║ 1 ║ 110 ║ NaN ║ NaN ║
║ 1 ║ 115 ║ NaN ║ NaN ║
║ 2 ║ 50 ║ 100 ║ NaN ║
║ 2 ║ 50 ║ NaN ║ NaN ║
║ 3 ║ 100 ║ 100 ║ NaN ║
║ 3 ║ 125 ║ NaN ║ NaN ║
╚══════╩════════╩══════╩════════╝
I would need to apply some math operations to fill FAB and Result:
For example to fill the first row of Result's column I'd have to:
df['Result'].iloc[0] = df['FAB'].iloc[0]*1.025 + df['Amount'].iloc[0]
This is equals to 305, so we should paste that result into the first row of result and the second for FAB
╔══════╦════════╦══════╦════════╗
║ User ║ Amount ║ FAB ║ Result ║
╠══════╬════════╬══════╬════════╣
║ 1 ║ 100 ║ 200 ║ 305 ║
║ 1 ║ 110 ║ 305 ║ NaN ║
║ 1 ║ 115 ║ NaN ║ NaN ║
║ 2 ║ 50 ║ 100 ║ NaN ║
║ 2 ║ 50 ║ NaN ║ NaN ║
║ 3 ║ 100 ║ 100 ║ NaN ║
║ 3 ║ 125 ║ NaN ║ NaN ║
╚══════╩════════╩══════╩════════╝
The same logic for the next row:
df['Result'].iloc[1] = df['FAB'].iloc[1]*1.025 + df['Amount'].iloc[1]
Which is equals to 422.62
╔══════╦════════╦═════════╦═════════╗
║ User ║ Amount ║ FAB ║ Result ║
╠══════╬════════╬═════════╬═════════╣
║ 1 ║ 100 ║ 200 ║ 305 ║
║ 1 ║ 110 ║ 305 ║ 422.625 ║
║ 1 ║ 115 ║ 422.625 ║ NaN ║
║ 2 ║ 50 ║ 100 ║ NaN ║
║ 2 ║ 50 ║ NaN ║ NaN ║
║ 3 ║ 100 ║ 100 ║ NaN ║
║ 3 ║ 125 ║ NaN ║ NaN ║
╚══════╩════════╩═════════╩═════════╝
So, after applying the same logic for all users
╔══════╦════════╦═════════╦════════════╗
║ User ║ Amount ║ FAB ║ Result ║
╠══════╬════════╬═════════╬════════════╣
║ 1 ║ 100 ║ 200 ║ 305 ║
║ 1 ║ 110 ║ 305 ║ 422.625 ║
║ 1 ║ 115 ║ 422.625 ║ 548.190625 ║
║ 2 ║ 50 ║ 100 ║ 152.5 ║
║ 2 ║ 50 ║ 152.5 ║ 206.3125 ║
║ 3 ║ 100 ║ 100 ║ 202.5 ║
║ 3 ║ 125 ║ 202.5 ║ 332.5625 ║
╚══════╩════════╩═════════╩════════════╝
Is there a way to acchieve this without a for loop?