我有以下DataFrame:
ID | Parent ID | Direction
1 | 0 | North
2 | 1 | South
3 | 1 | West
4 | 0 | East
我想编写一个函数,它将改变所有具有非零'父ID'的行的方向。到相应父级的方向。
期望的结果:
ID | Parent ID | Direction
1 | 0 | North #Ignore because Parent Id = 0
2 | 1 | North #Changed to Direction of Parent Id = 1
3 | 1 | North #Changed to Direction of Parent Id = 1
4 | 0 | East #Ignore because Parent Id = 0
以下是创建上述示例dataFrame的代码:
df = pd.DataFrame ({'ID' : [1,2,3,4],
'Parent ID' : [0,1,1,0],
'Direction' : ['North', 'South','West','East']})
我尝试编写一个函数并在父ID上调用map但是我没有成功。