Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe merge with update data

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
like image 523
Artem Ivanov Avatar asked Feb 09 '17 14:02

Artem Ivanov


1 Answers

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
like image 115
jezrael Avatar answered Sep 22 '22 10:09

jezrael