Home > Blockchain >  How do I multiply a smaler dataframe to evy row of another dataframe?
How do I multiply a smaler dataframe to evy row of another dataframe?

Time:01-19

I have 2 dataframes with the same column name but different amount of rows

data=ones((100,2))
df1=pd.DataFrame(data=data, columns = ('a','b'))

df2=pd.DataFrame({'a':[5,6,7,8,9],'b':[6,6,6,6,6]})

What I want to do is, multiply df2 to df1 but the df2 to repeat every 5 rows to the df1

What I did is

df6=pd.DataFrame(columns=('a','b'))
for x in range(0,90,5):    
    df6=df6.append(df2*df1.iloc[x:x 5,:])

But it only works for the first 5 rows an then I only get NaN

CodePudding user response:

After adding .values to your indexed dataframe your code works.

df6=df6.append(df2.multiply(df1.iloc[x:x 5,:].values))

CodePudding user response:

You can do reindex then mul

df1 = df1.mul(df2.reindex(df1.index%5).values)
  •  Tags:  
  • Related