'How to replace columns based on another column (matching parent id with id) python
I have a dataset that looks like this
| ID | TITLE | PARENT_ID |
|---|---|---|
| 1 | Tom | NaN |
| 2 | Lisa | 1 |
| 3 | Lecy | 1 |
| 4 | Ann | NaN |
| 5 | John | NaN |
| 6 | Lana | 4 |
If Lisa's PARENT_ID is 1, then her parent is Tom. If tom has NaN parent_id, means he is not child, he is parent. So that, I need it to look like this:
| ID | TITLE | PARENT_ID |
|---|---|---|
| 1 | Tom | NaN |
| 2 | Lisa | Tom |
| 3 | Lecy | Tom |
| 4 | Ann | NaN |
| 5 | John | NaN |
| 6 | Lana | Ann |
Solution 1:[1]
you can use map
df['PARENT_ID_FINAL']=df['ID'].map(dict(zip(df['ID'],df['TITLE'])))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | smart lph |
