Simple pandas solution
I started by using loc to find rows where p_id is NOT null and id is NOT in p_id. We create a \'type\' column and these rows are assigned the value \'Leaf\'\n\n
I started by using loc to find rows where p_id is NOT null and id is NOT in p_id. We create a \'type\' column and these rows are assigned the value \'Leaf\'\n\nNext, I used loc again to find rows where id IS in p_id. These got the \'type\' value \'Inner\' (Note: this also changed the \'Root\' rows, but those get changed in the last step.)\n\nLast, I used loc once again to find any row with the p_id that is null. These had the \'type\' changed to \'Root\'. And, return the dataframe with the p_id column dropped for submission.\n\n\n\n# Code\n```\nimport pandas as pd\ndef tree_node(tree: pd.DataFrame) -> pd.DataFrame:\n    tree.loc[~(tree[\'p_id\'].isna()) & ~(tree[\'id\'].isin(tree[\'p_id\'])), \'type\'] = \'Leaf\'\n    tree.loc[(tree[\'id\'].isin(tree[\'p_id\'])), \'type\'] = \'Inner\'\n    tree.loc[tree[\'p_id\'].isna(), \'type\'] = \'Root\'\n    return tree.drop(columns=\'p_id\')\n\n```
['Pandas']