如何在Python中对父子数据框中的值求和?
我的python笔记本中有以下DF
Account Rubriek debet credit parent
0 0 Vaste activa NaN NaN null
1 0.0 Materiele... NaN NaN 0
2 0.0.0 Terreinen 50000.0 NaN 0.0
3 0.0.1 Gebouwen 150000.0NaN 0.0
4 0.0.2 Verbouwi.. 20000.0 NaN 0.0
5 0.0.3 Machines NaN NaN 0.0
6 0.0.4 Gereedschappen NaN NaN 0.0
7 0.0.5 Inventaris NaN NaN 0.0
8 0.0.6 Wagenpark NaN NaN 0.0
9 0.1 Cummulitieve NaN NaN 0
我想以父帐户的累积值结束。我知道这可以通过递归来完成。我有以下内容:
从理论上讲,我认为算法应如下所示:
if account is leaf node:
debet_sum = debet
else
debet_sum = sum of debet_sum where account == parent
我可以通过以下方式确定帐户是否为叶节点:
df1['Account'].isin(df1['parent'])
第1行的debet_sum值可通过以下方式检索:
df1[df1.iloc[1]['Account'] == df1['parent']]['debet'].sum()
我第一次尝试将其组合如下:
def get_debet_sum(row):
if not df1['Account'].isin(df1['parent']):
debet_sum = df1.debet
else
debet_sum = df1[df1['Account'] == df1['parent']]['debet'].sum()
df1.assign(debet_sum = df1.apply(get_debet_sum, axis = 1))
但是,这没有给我任何结果。我没有下一步要做的最终结果就是我想要的