0

I have two dataframes looks like:

   col1  
0  1     

   col2  
0  2
1  3
2  4
3  5
4  6  

My goal is to fill col1 using the numbers from col2:

   col1      col2
0  1          2
1  2(=1x2)    3   
2  6(=2x3)    4
3  24(=6x4)   5
4  102(=24x5) 6 

So, that number in col1 calculated as the product of the value in col1 for the previous row and value in col2 for the previous row.

Stas Buzuluk
  • 794
  • 9
  • 19
Bubblethan
  • 379
  • 3
  • 11
  • This question is similar and should get you in the right direction: https://stackoverflow.com/questions/34855859/is-there-a-way-in-pandas-to-use-previous-row-value-in-dataframe-apply-when-previ – jpf5046 Jan 10 '20 at 12:55
  • @Bubblethan It would be great if you can mark any answer which satisfying you as correct. If you still need some clarifications - feel free to ask about it. – Stas Buzuluk Jan 10 '20 at 15:41

3 Answers3

1

I think numba is way how working with loops here if performance is important:

@jit(nopython=True)
def func(a, b):
    res = np.empty(a.shape)
    res[0] = b
    for i in range(1, a.shape[0]):
        res[i] = res[i-1] * a[i-1]
    return res

df2['col1'] = func(df2['col2'].values, df1.loc[0, 'col1'])

print (df2)
   col2   col1
0     2    1.0
1     3    2.0
2     4    6.0
3     5   24.0
4     6  120.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You may use shift and cumulative product here, without iterating, like this:

import pandas as pd

# Lets create artificial data
df_1 = pd.DataFrame() 
df_2 = pd.DataFrame() 

df_1['col_1'] = [1]
df_2['col_2'] = [2,3,4,5,6]

# Now lets add col_1 to df_2 
df_2['col_1'] = df_1['col_1']

# And fill all nans in the way you want
df_2['col_1'].fillna(df_2['col_2'].shift(1).cumprod(), inplace = True)
Stas Buzuluk
  • 794
  • 9
  • 19
1
for i in range(1,len(df2)):
    df1.at[i,'col1'] = df1.col1[i-1] * df2.col2[i-1]

df1['col2'] = df2.col2
>>> df1
    col1  col2
0    1.0     2
1    2.0     3
2    6.0     4
3   24.0     5
4  120.0     6
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37