Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading