1

I am trying to merge two pandas dataframe with different number of row also there are some index values which are common to both pandas and some which are not. I want to create a combined dataframe.

I am using 'concat' method to achieve the results

 result=pd.concat([df1,df2]).sort_index()

For e.g. i have the following two dataframes (df1 and df2) as following

index         value1
2019-01-01    1
2019-02-01    2
2019-03-01    3
2019-04-01    4
2019-05-01    5
2019-12-01    17

index          value2
2019-01-01     2
2019-02-01     6
2019-08-01     9
2019-09-01     7.5
2019-10-01     11

And want to get this result:

 index      value1    value2
2019-01-01    1       2
2019-02-01    2       6
2019-03-01    3       NaN
2019-04-01    4       NaN   
2019-05-01    5       NaN
2019-08-01    NaN     9
2019-09-01    NaN     7.5
2019-10-01    NaN      11
2019-12-01    17      NaN

But i get something like this when i use the above 'concat' where the index which are common are duplicated instead of getting merged.

  index      value1  value2
2019-01-01    1       NaN
2019-01-01    NaN       2
2019-02-01    2       NaN
2019-02-01    NaN       6
2019-03-01    3       NaN
2019-04-01    4       NaN   
2019-05-01    5       NaN
2019-08-01    NaN       9
2019-09-01    NaN     7.5
2019-10-01    NaN      11
2019-12-01    17      NaN

Can anyone please point out what i am doing wrong here or how can i get my desired result?

ALollz
  • 57,915
  • 7
  • 66
  • 89
RisingSun
  • 123
  • 1
  • 1
  • 7
  • is `index` your actual index or a column named index? If the latter, use `sort_values(by='index')` and not `.sort_index()` – rafaelc Oct 04 '19 at 14:50

0 Answers0