数据框比我正在使用的要简单一些,但是我正在尝试编写一个函数,该函数将查看我们拥有资金的三个不同预算。无论预算如何,我们的每日支出率都是一致的,但是希望从第一个预算,第二个第二预算等等开始。我已经开始编写该函数,但是已经遇到了很多问题,而没有陷入更大的复杂性。我尝试创建的功能将使支出的预算减少,并在“已花费”列中返回相应的值。如果支出金额超出预算,它将从第二个预算(然后是第三个预算)中提取,分别递减。
FullName
答案 0 :(得分:0)
尝试一下:
def funded_calc(df):
s = 0
df['Spent 1'] = df['Budget 1'] - df['Spend Rate']*df['Days']
if df['Spent 1'] < 0:
s = df['Spent 1']
df['Spent 1'] = 0
df['Spent 2'] = df['Budget 2'] - df['Spend Rate']*df['Days'] + s
if df['Spent 2'] < 0:
s = df['Spent 2']
df['Spent 2'] = 0
df['Spent 3'] = df['Budget 3'] - df['Spend Rate']*df['Days'] + s
df['Total Spent'] = sum([df['Spent 1'], df['Spent 2'], df['Spent 3']])
return df
df.apply(funded_calc, axis=1)
输出:
Budget 1 Budget 2 Budget 3 Days Fiscal Year ID Spend Rate Spent 1 \
0 1000 1000 1000 250 2018 1 2.5 375.0
1 1200 1200 1200 250 2018 2 3.0 450.0
2 1300 1300 1300 250 2018 3 4.0 300.0
3 100 100 100 250 2018 4 5.0 0.0
4 500 500 500 250 2018 5 4.0 0.0
5 0 0 0 250 2018 6 10.0 0.0
6 800 800 800 250 2018 7 2.5 175.0
7 950 950 950 250 2018 8 2.0 450.0
8 4321 4321 4321 250 2018 9 4.0 3321.0
9 800 800 800 250 2018 10 2.5 175.0
10 1000 1000 1000 250 2019 1 2.5 375.0
11 1200 1200 1200 250 2019 2 3.0 450.0
12 1300 1300 1300 250 2019 3 4.0 300.0
13 100 100 100 250 2019 4 5.0 0.0
14 500 500 500 250 2019 5 4.0 0.0
15 0 0 0 250 2019 6 10.0 0.0
16 800 800 800 250 2019 7 2.5 175.0
17 950 950 950 250 2019 8 2.0 450.0
18 4321 4321 4321 250 2019 9 4.0 3321.0
19 800 800 800 250 2019 10 2.5 175.0
Spent 2 Spent 3 Total Spent
0 375.0 375.0 1125.0
1 450.0 450.0 1350.0
2 300.0 300.0 900.0
3 0.0 -3450.0 -3450.0
4 0.0 -1500.0 -1500.0
5 0.0 -7500.0 -7500.0
6 175.0 175.0 525.0
7 450.0 450.0 1350.0
8 3321.0 3321.0 9963.0
9 175.0 175.0 525.0
10 375.0 375.0 1125.0
11 450.0 450.0 1350.0
12 300.0 300.0 900.0
13 0.0 -3450.0 -3450.0
14 0.0 -1500.0 -1500.0
15 0.0 -7500.0 -7500.0
16 175.0 175.0 525.0
17 450.0 450.0 1350.0
18 3321.0 3321.0 9963.0
19 175.0 175.0 525.0
是您想要的吗?
我使用每种原始材料申请。