如何在Python DF中使用递归和?
我希望最终得到帐户层次结构中所有子值的总和。为此,我有以下基本代码:
import numpy as np
import pandas as pd
#load csv into pandas data frame
df1 = pd.read_csv(r"accounts.csv")
xi = 0
xn = 10
#Append parent based on account
df1 = df1.assign(parent=df1['Account']\
.str.split('.')\
.apply(lambda x: '.'.join(x[:-1]) or 'null'))\
.sort_values(['Account'], ascending=[True])
df1[xi:xn]
这将产生以下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
我想要一个带有debet_sum的额外列,该列显示所有子节点的值。因此,节点0.0 debet_sum = 220.000,节点0 debet_sum = 220.000。最终我想在信誉方面也做同样的事情。
我尝试了以下操作,但似乎不起作用
def debetsum (x):
if df1.parent == 'null':
df1.assign(Debet_sum=debit_sum)
else:
df1.assign(debet_sum=debit + debit_sum)