Arshbir Sandhu
Arshbir Sandhu

Reputation: 121

How to inner join in pandas as SQL , Stuck in a problem below

I have two df named "df" and second as "topwud".

df

topwud

when I join these two dataframes bt inner join using BOMCPNO and PRTNO as the join column

like

second_level=pd.merge(df,top_wud ,left_on='BOMCPNO', right_on='PRTNO', how='inner').drop_duplicates()

Then I got this data frame

Result

I don't want common name coming as PRTNO_x and PRTNO_y , I want to keep only PRTNO_x in my result dataframe as name "PRTNO" which is default name. Kindly help me :)

Upvotes: 1

Views: 835

Answers (1)

DJKarma
DJKarma

Reputation: 182

try This -

pd.merge(df1, top_wud, on=['BOMCPNO', 'PRTNO'])

What this will do though is return only the values where BOMCPNO and PRTNO exist in both dataframes as the default merge type is an inner merge.

So what you could do is compare this merged df size with your first one and see if they are the same and if so you could do a merge on both columns or just drop/rename the _x/_y suffix B columns.

I would spend time though determining if these values are indeed the same and exist in both dataframes, in which case you may wish to perform an outer merge:

pd.merge(df1, df2, on=['A', 'B'], how='outer')

Then what you could do is then drop duplicate rows (and possibly any NaN rows) and that should give you a clean merged dataframe.

merged_df.drop_duplicates(cols=['BOMCPNO', 'PRTNO'],inplace=True)

also try other types of join , as i dont know what exactly you want, i think its left inner .

check this if it solved your problem.

Upvotes: 1

Related Questions