首先,您需要更正python代码
MgrID列表中的错字:
[0,1,1,2,0,0,5,6]
其次,如果这项工作是在SQL中以递归方式完成的,为什么您期望Python / Pandas可以不用递归方法来完成呢?并不难:
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']]
然后,output(
nLevel)是您所需要的(顺序除外,我从您的SQL中无法理解该顺序):
nLevel ID NameID 1 1 1 Keith2 2 2 Josh3 2 3 Robin4 3 4 Raja5 1 5 Tridip6 1 6 Arijit7 2 7 Amit8 2 8 Dev[8 rows x 3 columns]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)