I have two Dataframes which I want to merge on the column CCOL: I am giving a short sample as follows: ( the same name Cols have the same data in both dataframes)
df1 =
CCOL ColA ColB ColC ColD ColE ColF
A 1.0 a b 23 45 2.7
B 3.0 c q 26 34 5.9
C 5.0 f r 2 4.9 15.9
D 7.0 e s 45 2 8
df2 =
CCOL ColA ColB ColE ColP ColR ColS
A 1.0 a 45 vq 4 21.7
AF 7.0 za 7.9 r 4 3.1
D 7.0 e 2 gh 3 53.1
GK 8.0 ut 2 qt 4 33.3
So the final output from these two dataframes I am looking for is like:
final_df =
CCOL ColA ColB ColC ColD ColE ColF ColP ColR ColS
A 1.0 a b 23 45 2.7 vq 4 21.7
D 7.0 e s 45 2 8 gh 3 53.1
I tried
newdf = pd.merge(df1,df2,on='CCOL', how='inner')
However, this one creates ColA_x, ColA_y etc in the new df from same columns, which I do not need.
My tries:,
newdf = df1.combine_first(df2)
However, this one creates a dataframe like the follows which gives correct data for rows with CCOL, "A" & "D" but totally wrong for the columns which df1 & df2 does not match on CCOL.
newdf =
CCOL ColA ColB ColC ColD ColE ColF ColP ColR ColS
A 1.0 a b 23 45.0 2.7 vq 4.0 21.7
B 3.0 c q 26 34.0 5.9 r 4.0 3.1
C 5.0 f r 2 4.9 15.9 gh 3.0 53.1
D 7.0 e s 45 2.0 8.0 qt 4.0 33.3
So not understanding how to achieve my output final_df. Any help will be very much appreciated.