how to merge two data frames based on particular column in pandas python?
Use merge
:
print (pd.merge(df1, df2, on='company'))
Sample:
print (df1) company standard0 tata A11 cts A22 dell A3print (df2) company return0 tata 711 dell 782 cts 273 hcl 23print (pd.merge(df1, df2, on='company')) company standard return0 tata A1 711 cts A2 272 dell A3 78
In order to successfully merge two data frames based on common column(s), the dtype for common column(s) in both data frames must be the same! dtype for a column can be changed by:
df['commonCol'] = df['commonCol'].astype(int)