Home > database >  Add a portion of a dataframe to another dataframe
Add a portion of a dataframe to another dataframe

Time:12-16

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!

CodePudding user response:

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

CodePudding user response:

How about:

idx = df1.shape[0] - df2.shape[0]
df1[idx:] = df1[idx:].values   df2.values

Output:

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