2

I have a pandas data frame(df1) and I need to replace some of the df1 values with another data frame (df2). the df1 contains a time series from 1998-01-01 to 2002-12-31 and df1 contains time series from 1998-03-01 to 1998-07-31.

I want to replace the df1 values with df2 values for the time period of df2 (i.e. 1998-03-01 to 1998-07-31)

df1=

date          kc
1998-01-01    0
1998-01-02    0
1998-01-03    0
1998-01-04    0
1998-01-05    0
.
.
.
2002-12-30    0
2002-12-31    0

and df2=

date          kc
1998-03-01    0.3
1998-03-02    0.35
1998-03-03    0.4
1998-03-04    0.45
1998-03-05    0.4
.
.
.
1998-07-30    0.6
1998-07-31    0.7

Where the date column is set index for both of the dataframes. I tried the following:

df1.loc["1998-03-01":"1998-07-31","kc"]=df2

But it changes nothing, df1 remains same.

1 Answer 1

2

Samples:

print (df1)
            kc
Date          
1998-01-01   0
1998-02-01   0
1998-03-01   0
1998-03-02   0
1998-03-03   0
2002-12-30   0
1998-12-31   0

print (df2)
              kc
date            
1998-02-01  0.30
1998-03-01  0.35
1998-03-02  0.40
1998-03-03  0.45
1998-03-04  0.40
2002-07-30  0.60
1998-07-31  0.70

You can use Series.combine_first

df1["kc"] = df2['kc'].combine_first(df1['kc'])

print (df1)
              kc
Date            
1998-01-01  0.00
1998-02-01  0.30
1998-03-01  0.35
1998-03-02  0.40
1998-03-03  0.45
2002-12-30  0.00
1998-12-31  0.00

Or Index.isin for new values by mask:

df1.loc[df1.index.isin(df2.index), "kc"]=df2['kc']

print (df1)
              kc
Date            
1998-01-01  0.00
1998-02-01  0.30
1998-03-01  0.35
1998-03-02  0.40
1998-03-03  0.45
2002-12-30  0.00
1998-12-31  0.00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.