1

Problem

I have the following dataframe

p = {'parentId':['071cb2c2-d1be-4154-b6c7-a29728357ef3', 'a061e7d7-95d2-4812-87c1-24ec24fc2dd2', 'Highest Level', '071cb2c2-d1be-4154-b6c7-a29728357ef3'],
     'id_x': ['a061e7d7-95d2-4812-87c1-24ec24fc2dd2', 'd2b62e36-b243-43ac-8e45-ed3f269d50b2', '071cb2c2-d1be-4154-b6c7-a29728357ef3', 'a0e97b37-b9a1-4304-9769-b8c48cd9f184'],
    'type': ['Department', 'Department', 'Department', 'Function'], 'name': ['Sales', 'Finances', 'Management', 'Manager']}
df = pd.DataFrame(data = p)
df

| parentId                             | id_x                                 | type       | name       |
| ------------------------------------ | ------------------------------------ | ---------- | ---------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | Department | Sales      |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | Department | Finances   |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | Department | Management |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | Function   | Manager    |

I tried to create a function that should return the name of the corresponding entry, where the parentId is the id_xand put it in a new column. With the function I get the following result:

def allocator(id_x, parent_ID, name):
    d = "no sub-dependency"
    for node in id_x:
        if node == parent_ID:
            d = name
    return d

df['Parent_name'] = df.apply(lambda x: allocator(df['id_x'], x['parentId'], x['name']), axis=1)
df

| parentId                             | id_x                                 | type       | name       | Parent_name       |
| ------------------------------------ | ------------------------------------ | ---------- | ---------- | ----------------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | Department | Sales      | Sales             |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | Department | Finances   | Finances          |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | Department | Management | no sub-dependency |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | Function   | Manager    | Manager           |

Expected result

The function up to now only puts in the name of the corresponding id_x itself. However, it should take the name of the entry where the parentId is the id_x.

 
| parentId                             | id_x                                 | type       | name       | Parent_name       |
| ------------------------------------ | ------------------------------------ | ---------- | ---------- | ----------------- |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | Department | Sales      | Management        |
| a061e7d7-95d2-4812-87c1-24ec24fc2dd2 | d2b62e36-b243-43ac-8e45-ed3f269d50b2 | Department | Finances   | Sales             |
| Highest Level                        | 071cb2c2-d1be-4154-b6c7-a29728357ef3 | Department | Management | no sub-dependency |
| 071cb2c2-d1be-4154-b6c7-a29728357ef3 | a0e97b37-b9a1-4304-9769-b8c48cd9f184 | Function   | Manager    | Management        |

How do I have to change the function, so it takes the name of the related parent entry?

2
  • In the expected result, i think the output at row 2 should be Sales. Commented Apr 19, 2021 at 10:28
  • @ShubhamSharma - yes, sorry. I corrected it. Commented Apr 19, 2021 at 10:31

1 Answer 1

1

You can use .map():

mapping = dict(zip(df["id_x"], df["name"]))
df["Parent_name"] = df["parentId"].map(mapping).fillna("no sub-dependency")
print(df)

Prints:

                               parentId                                  id_x        type        name        Parent_name
0  071cb2c2-d1be-4154-b6c7-a29728357ef3  a061e7d7-95d2-4812-87c1-24ec24fc2dd2  Department       Sales         Management
1  a061e7d7-95d2-4812-87c1-24ec24fc2dd2  d2b62e36-b243-43ac-8e45-ed3f269d50b2  Department    Finances              Sales
2                         Highest Level  071cb2c2-d1be-4154-b6c7-a29728357ef3  Department  Management  no sub-dependency
3  071cb2c2-d1be-4154-b6c7-a29728357ef3  a0e97b37-b9a1-4304-9769-b8c48cd9f184    Function     Manager         Management
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.