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

Add a portion of a dataframe to another dataframe

Suppose to have two dataframes, df1 and df2, with equal number of columns, but different number of rows, e.g:

df1 = pd.DataFrame([(1,2),(3,4),(5,6),(7,8),(9,10),(11,12)], columns=['a','b'])

       a   b
   1   1   2
   2   3   4
   3   5   6
   4   7   8
   5   9   10
   6   11  12
df2 = pd.DataFrame([(100,200),(300,400),(500,600)], columns=['a','b'])

        a     b
   1   100   200
   2   300   400
   3   500   600

I would like to add df2 to the df1 tail (df1.loc[df2.shape[0]:]), thus obtaining:

        a     b
   1    1     2
   2    3     4
   3    5     6
   4   107   208
   5   309   410
   6   511   612

Any idea?
Thanks!

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

>Solution :

If there is more rows in df1 like in df2 rows is possible use DataFrame.iloc with convert values to numpy array for avoid alignment (different indices create NaNs):

df1.iloc[-df2.shape[0]:] += df2.to_numpy()
print (df1)
     a    b
0    1    2
1    3    4
2    5    6
3  107  208
4  309  410
5  511  612

For general solution working with any number of rows with unique indices in both Dataframe with rename and DataFrame.add:

df = df1.add(df2.rename(dict(zip(df2.index[::-1], df1.index[::-1]))), fill_value=0)
print (df)

       a      b
0    1.0    2.0
1    3.0    4.0
2    5.0    6.0
3  107.0  208.0
4  309.0  410.0
5  511.0  612.0
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