Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

pandas - Adding 2 columns of time series dataframes with different dates

I am looking to add two columns with different date range

column 1 = values with date index 2 Nov to 23 Nov
column 2 = values with date index 27 Oct  to 17 Nov

Resultant = addition of values in column 1 and column 2 of 27 Oct to 23 Nov

Sample pic attached

enter image description here

  1. Column 1 of dataframeA has data from 2 Nov to 23 Nov; each element has value 100
  2. Column 2 of dataframe B has data from 27 Oct to 17 Nov; each element has value 200
  3. Result will be data sum of these columns with all date included.
question from:https://stackoverflow.com/questions/65856581/adding-2-columns-of-time-series-dataframes-with-different-dates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

df1:

    Date        Value
0   2-11-2020   21.0
1   3-11-2020   4.0
2   4-11-2020   6.0

df2:

    Date        Value
0   3-11-2020   2.0
1   4-11-2020   2.0
2   5-11-2020   7.0

It should be:

df = df1.set_index('Date').add(df2.set_index('Date'), fill_value=0).reset_index()

df:

    Date        Value
0   2-11-2020   21.0
1   3-11-2020   6.0
2   4-11-2020   8.0
3   5-11-2020   7.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...