0

I have a question. I have two data set as under,

df1

Sl No  Address  
1      1111
2      2222 
3      2345
4      7890
5      0987
6      3456 
7      1233 
df2

email  Add.
AA    A123
AA    1111 
AA    99999
BB    a9999
BB    345689
BB    345699 
CC    1233 

I'm trying to merge the two dataframe based on address column and bring column named email to the df1.

I have renamed the column, and passed the merge function as under.

df2.rename(columns = {'Add.':'Address'}, inplace = True)
df1 = df1.merge(df2['email'],how="left", on = "Address")

I'm not sure why but i'm getting a key error

~\anaconda3\lib\site-packages\pandas\core\generic.py in _get_label_or_level_values(self, key, axis)
   1682             values = self.axes[axis].get_level_values(key)._values
   1683         else:
-> 1684             raise KeyError(key)
   1685 
   1686         # Check for duplicates

KeyError: 'Address'

I verified the files, column named "Address" is present in the source file. Not sure why merge function is saying otherwise. Note - Both the address columns in df1 and df2 are objects

Help would be appreciated!

Sid
  • 163
  • 7

1 Answers1

2

Do not select the email column when you merge , this will make the merged df become series

df1 = df1.merge(df2[['Address','email']], how = "left", on = "Address")
BENY
  • 317,841
  • 20
  • 164
  • 234
  • I would want only email column to be merged to df1, bcoz df2 has a lot of irrelevant columns which would make the file size large to run other analysis – Sid Jun 03 '22 at 14:04
  • @Sid select your column for df2 , check the update – BENY Jun 03 '22 at 14:16
  • I used the above code, but getting a key error, if use ```df1 = df1.merge(df2, how = "left", on = "Address")``` it works but when I use the code suggested I get key error. completely not sure why – Sid Jun 03 '22 at 14:19
  • @Sid it work on my side.... – BENY Jun 03 '22 at 14:20