Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Performing VLOOKUP in Python is not giving me correct result

I have below data in Python.

dict1 = {"Col1" : (1790212,2232831,1910525,2158628,1751623,2246989,2157180,2041263)}
dict2 = {"Col1" : (2041263,2488561,2305036,2142484,1751623,2246989,2157180,1790212)}
a = pd.DataFrame(dict1)
b = pd.DataFrame(dict2)

After this I perform VLOOKUP on the above 3 data frame using pd.merge() function

c = pd.merge(a,b, on = 'Col1', how = 'left')

OUTPUT of the above code is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

|    | COL1  |
|----|-------|
| 0  |1790212|
| 1  |2232831|
| 2  |1910525|
| 3  |2158628|
| 4  |1751623|
| 5  |2246989|
| 6  |2157180|
| 7  |2041263|

The value displayed is incorrect because 2232831,1910525,2158628 are not present in B Data frame it should have displayed NA value instead of this.

Kindly let me know if I have missed anything?

>Solution :

There are same columns names, so left join return original column, so seems no left join. If rename column all working like need:

c = pd.merge(a,
             b.rename(columns={'Col1':'Col2'}), 
             left_on = 'Col1',
             right_on='Col2', 
             how = 'left')
print (c)
      Col1       Col2
0  1790212  1790212.0
1  2232831        NaN
2  1910525        NaN
3  2158628        NaN
4  1751623  1751623.0
5  2246989  2246989.0
6  2157180  2157180.0
7  2041263  2041263.0

Or is possible use indicator parameter for see how merging working:

c = pd.merge(a,b, on = 'Col1', how = 'left', indicator=True)
print (c)
      Col1     _merge
0  1790212       both
1  2232831  left_only
2  1910525  left_only
3  2158628  left_only
4  1751623       both
5  2246989       both
6  2157180       both
7  2041263       both
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading