I have excel sheet that I update with quarterly financial data. I want to automate the process of updating it each quarter

Question:

so I have two df one with time series of historical data (df1) the other with the recent year data (df2)

so for example 
df(1):
 Date   DebtGDP   Credit    Expenditure
 2020  0.053078  0.501184    757.000000  
 2021  0.935433  0.050437    229.000000 
 2022      NaN       NaN        NaN
 2023      NaN       NaN        NaN

df(2):
 Date   DebtGDP   Credit    Expenditure
 2022  0.056078  0.401184    757.000000
 2023  0.605842  0.661184    900.000000

I want df1 to read from df2 and update the recent ones


Wanted output:

df(1) would be:
 Date   DebtGDP   Credit    Expenditure
 2020  0.053078  0.501184    757.000000  
 2021  0.935433  0.050437    229.000000 
 2022  0.056078  0.401184    757.000000
 2023  0.605842  0.661184    900.000000
Asked By: PyPixels

||

Answers:

You can merge and combine_first:

out = df1.combine_first(df2.merge(df1[['Date']], how='right'))

Or:

out = df1.set_index('Date').combine_first(df2.set_index('Date')).reset_index()

Output:

   Date   DebtGDP    Credit  Expenditure
0  2020  0.053078  0.501184        757.0
1  2021  0.935433  0.050437        229.0
2  2022  0.056078  0.401184        757.0
3  2023  0.605842  0.661184        900.0
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.