【问题标题】:Building a function that can iterate through rows and pull from different columns构建一个可以遍历行并从不同列中提取的函数
【发布时间】:2018-09-11 14:21:34
【问题描述】:

数据框比我正在使用的要简单一些,但我正在尝试编写一个函数来查看我们拥有资金的三个不同预算。无论预算如何,我们的每日支出率都是一致的,但首先要从第一个预算中提取,第二个是第二个,等等。我已经开始编写函数,但已经遇到问题而没有涉及更大的复杂性.我尝试创建的函数将通过花费的任何内容来减少相应的预算,并在“花费”列中返回相应的值。如果要花费的金额超过预算,它将从第二个预算(然后是第三个)中提取,分别递减。

import pandas as pd
import matplotlib.pyplot as plt

ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Budget_1 = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800, 1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
Budget_2 = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800, 1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
Budget_3 = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800, 1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
FY = [2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019]
Days = [250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250, 250]
spend = [2.5, 3.0, 4.0, 5.0, 4.0, 10.0, 2.5, 2.0, 4.0, 2.5, 2.5, 3.0, 4.0, 5.0, 4.0, 10.0, 2.5, 2.0, 4.0, 2.5]
Spent_1 = ''
Spent_2 = ''
Spent_3 = ''
Total_Spent = ''
d = {'ID': ID, 'Budget 1': Budget_1, 'Budget 2': Budget_2, 'Budget 3': Budget_3, 'Fiscal Year': FY, 'Days': Days, 'Spent 1': Spent_1, 'Spent 2': Spent_2, 'Spent 3': Spent_3, 'Total Spent': Total_Spent, 'Spend Rate': spend}
df = pd.DataFrame(d)

print(df)

df = df.sort_values(['Fiscal Year', 'ID'], ascending = True)

def funded_calc(spendrate):
    spent1 = spendrate*df['Days']
    df['Spent 1'] = df['Spent 1'] - spent1
    return spent1

df['Spent'] = df['Spend Rate'].apply(funded_calc)

print(df)

【问题讨论】:

  • 那么你的问题是什么?有点不清楚您的问题出在哪里。
  • 您希望从这些输入数据中得到什么结果?

标签: python python-3.x pandas


【解决方案1】:

试试这个:

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 

这是你想要的吗?

我对每个 raw 都使用 apply。

【讨论】:

  • 我的代码无法重现与您相同的结果。我仍然收到“已用 1-3”和“总计”的空白列。
  • 尝试 df = df.apply(funded_calc, axis=1) 保存结果。或者尝试在“=”之前将 .loc 添加到 df。比如:df.loc["Spent 1"] = df["Budget 1"] -... 等
猜你喜欢
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
  • 2016-08-09
  • 2020-07-18
  • 1970-01-01
  • 2019-12-17
相关资源
最近更新 更多