4

I hawe two DataFrame:

df1 = pd.DataFrame({'date':['2017-01-01','2017-01-02','2017-01-03','2017-01-04','2017-01-05'], 'value':[1,1,1,1,1]})
df2 = pd.DataFrame({'date':['2017-01-04','2017-01-05','2017-01-06','2017-01-07','2017-01-08'], 'value':[2,2,2,2,2]})

date        value      date        value         
2017-01-01      1      2017-01-04      2
2017-01-02      1      2017-01-05      2
2017-01-03      1      2017-01-06      2
2017-01-04      1      2017-01-07      2
2017-01-05      1      2017-01-08      2

Need to merge df1 and df2 to obtain the following results:

date        value
2017-01-01      1
2017-01-02      1
2017-01-03      1
2017-01-04      2
2017-01-05      2
2017-01-06      2
2017-01-07      2
2017-01-08      2

2 Answers 2

5

You can use concat with drop_duplicates by column date and keep last values:

print (pd.concat([df1, df2]).drop_duplicates('date', keep='last'))
         date  value
0  2017-01-01      1
1  2017-01-02      1
2  2017-01-03      1
0  2017-01-04      2
1  2017-01-05      2
2  2017-01-06      2
3  2017-01-07      2
4  2017-01-08      2
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you can use the combine_first command built into pandas.

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.combine_first.html

in this case you would do

df3 = df1.combine_first(df2)

Im not certain if it works in the case you are replacing an integer with an integer or if you need to have NaN values in place.

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.