0

I have two dataframes below:

dfA = pd.DataFrame([[3,"here",34]],columns = ["comp_id","mail","mean"])
dfB = pd.DataFrame([[23,3,"there"]], columns = ["alt","name_id","serv"])

dfA

        comp_id  mail  mean
   0          3 "here"   34

dfB

          alt  name_id   serv
   0       23        3  "there"

I want to join both data frames on comp_id = name_id

Output:

        mail  mean   alt     serv
 0     "here"   34    23   "there"

The output should not include comp_id or name_id.

Any suggestions on how I can do this?

swing1234
  • 233
  • 1
  • 3
  • 13
  • 1
    Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – MrSoLoDoLo Jul 29 '20 at 17:16

3 Answers3

2

Use merge with left_on and right_on:

pd.merge(dfA,dfB,left_on="comp_id", right_on = "name_id", how="inner")
Let's try
  • 1,044
  • 9
  • 20
1

Use merge after setting index for first data frame and drop column from second data frame

dfA = dfA.set_index('comp_id')
res = dfA.merge(dfB,left_index=True,right_on='name_id').drop('name_id',axis=1)
print(res)
1

Drop you join keys after the merge:

dfA.merge(dfB, left_on='comp_id', right_on='name_id').drop(['comp_id','name_id'], axis=1)

Output:

   mail  mean  alt   serv
0  here    34   23  there
Scott Boston
  • 147,308
  • 15
  • 139
  • 187