0

I have two dataframes df1 and df2

df1 looks like:

a    b   
ravi     2  
ramesh   3
ganesh   4
kamlesh  8

df2 looks like:

a    c   d
harish  50  70 
ravi    80  60  
suresh  40  20  
kamlesh 90  100

when I'm merging the two df's df2 and df1 using an outer join(on column a) to include all the columns, the resultant dataframe having dtypes as int64 for columns c,d,b changes from int64 to float.

Is there any way of preserving the same dtypes as int after merging?

Sudip Roy
  • 3
  • 3
  • 1
    The same question has been asked and answered here: https://stackoverflow.com/questions/36743563/preserve-dataframe-column-data-type-after-outer-merge – Anja S Feb 08 '21 at 12:38
  • Thankyou @AnjaS for responding. I did came across this solution earlier, but it didn't work out.The solution depicts two dataframes with unequal columns and then it adds another column to maintain equal nos of columns in both df's. Then, they are joined by .join() method. While I don't want to add any extra column instead merge likewise (on the first column) so the resultant dataframe will have all the columns for both df's and the values for cols which are not present are replaced with NAN's and It can be achieved by a outer join to be specific. – Sudip Roy Feb 08 '21 at 13:51

1 Answers1

0

After join/merge use this:

df2.loc[:, ['c','d']] = df2.loc[:, ['c','d']].astype('Int64')
Romero_91
  • 405
  • 3
  • 15