how do I elegantly port the below recursive SQL query to Pandas python code? Somehow I don\'t see a straightforward way without writing own recursive function?
Pytho
First, you need to correct the typo in python code MgrID
list:
[0,1,1,2,0,0,5,6]
Second, if this job is done recursively in SQL, why do you expect Python/Pandas can do it without recursive method? It isn't too hard:
def nlevel(id, mgr_dict=df.MgrID, _cache={0:0}):
if id in _cache:
return _cache[id]
return 1+nlevel(mgr_dict[id],mgr_dict)
df['nLevel'] = df.ID.map(nlevel)
print df[['nLevel','ID','Name']]
Then the output(nLevel
) is what you need (except the order, which I don't understand from your SQL):
nLevel ID Name
ID
1 1 1 Keith
2 2 2 Josh
3 2 3 Robin
4 3 4 Raja
5 1 5 Tridip
6 1 6 Arijit
7 2 7 Amit
8 2 8 Dev
[8 rows x 3 columns]