Search code examples
pandasdataframemergeconcatenation

Merge 2 pandas dataframe while adding one column in 2nd dataframe to 1st dataframe


I have 2 dataframes

df1 =

    time    power
0   0.00    800.380000
1   0.01    788.281667
2   0.02    776.183333
3   0.03    764.085000
4   0.04    782.222500

df2 =

    time    power
0   0.00    1485.715
1   0.01    1485.915
2   0.02    1485.715
3   0.03    1450.980
4   0.04    1450.995

I want to merge both of them together as one and while merging I should add columns. It should look like this .

time    power
0   0.00    800.380000
1   0.01    788.281667
2   0.02    776.183333
3   0.03    764.085000
4   0.04    782.222500
5   0.04    1485.715
6   0.05    1485.915
7   0.06    1485.715
8   0.07    1450.980
9   0.08    1450.995

Can anyone please suggest some ideas ?


Solution

  • Use concat with add maximal time from df1 to df2:

    df = pd.concat([df1, df2.assign(time = df2['time'].add(df1['time'].max()))], 
                   ignore_index=True)
    print (df)
       time        power
    0  0.00   800.380000
    1  0.01   788.281667
    2  0.02   776.183333
    3  0.03   764.085000
    4  0.04   782.222500
    5  0.04  1485.715000
    6  0.05  1485.915000
    7  0.06  1485.715000
    8  0.07  1450.980000
    9  0.08  1450.995000