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

How to vlookup within a single dataframe?

From the following dataframe (df):

|------------+--------------------+-------------|
| child_code | child_name         | parent_code |
|------------+--------------------+-------------|
|        900 | World              |           0 |
|        920 | South-Eastern Asia |         900 |
|        702 | Singapore          |         920 |
|------------+--------------------+-------------|

I would like to produce this dataframe:

|------------+--------------------+-------------+--------------------|
| child_code | child_name         | parent_code | parent_name        |
|------------+--------------------+-------------+--------------------|
|        900 | World              |           0 |                    |
|        920 | South-Eastern Asia |         900 | World              |
|        702 | Singapore          |         920 | South-Eastern Asia |
|------------+--------------------+-------------+--------------------|```
How could I make the equivalent of an MS Excel `vlookup` to produce the `parent_name` column?

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

>Solution :

You can use Series.map:

import pandas as pd
import numpy as np

data = {'child_name': {0: 'World', 1: 'South-Eastern Asia', 2: 'Singapore'}, 
        'child_code': {0: 900, 1: 920, 2: 702}, 
        'parent_code': {0: 0, 1: 900, 2: 920}}
df = pd.DataFrame(data)

df['parent_name'] = df['parent_code'].map(df.set_index('child_code')['child_name'])

df

           child_name  child_code  parent_code         parent_name
0               World         900            0                 NaN
1  South-Eastern Asia         920          900               World
2           Singapore         702          920  South-Eastern Asia
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