So, I have the following Pandas DataFrame where all values in third column (Ratio) are the same:
import pandas as pd
df = pd.DataFrame([[2, 10, 0.5],
[float('NaN'), 10, 0.5],
[float('NaN'), 5, 0.5]], columns=['Col1', 'Col2', 'Ratio'])
╔══════╦══════╦═══════╗
║ Col1 ║ Col2 ║ Ratio ║
╠══════╬══════╬═══════╣
║ 2 ║ 10 ║ 0.5 ║
║ NaN ║ 10 ║ 0.5 ║
║ NaN ║ 5 ║ 0.5 ║
╚══════╩══════╩═══════╝
I want to know if there is a way to multiply Col1 * Ratio and then the output of that product add it to Col2 and append the value to next row Col1 using a function provided by pandas.
Output example:
╔══════╦══════╦═══════╗
║ Col1 ║ Col2 ║ Ratio ║
╠══════╬══════╬═══════╣
║ 2 ║ 10 ║ 0.5 ║
║ 11 ║ 10 ║ 0.5 ║
║ 15.5 ║ 5 ║ 0.5 ║
╚══════╩══════╩═══════╝
I think numba is way how working with loops here if performance is important:
from numba import jit
@jit(nopython=True)
def f(a, b, c):
for i in range(1, a.shape[0]):
a[i] = a[i-1] * c[i-1] + b[i-1]
return a
df['Col1'] = f(df['Col1'].to_numpy(), df['Col2'].to_numpy(), df['Ratio'].to_numpy())
print (df)
Col1 Col2 Ratio
0 2.0 10 0.5
1 11.0 10 0.5
2 15.5 5 0.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With