Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas/Python建模时间序列,具有不同输入的组_Python_Pandas - Fatal编程技术网

Pandas/Python建模时间序列,具有不同输入的组

Pandas/Python建模时间序列,具有不同输入的组,python,pandas,Python,Pandas,我试图为未来几年的资产组建模不同的场景。这是我在Excel中非常乏味地完成的事情,但我想利用我用Pandas构建的大型数据库 例如: annual_group_cost = 0.02 df1: year group x_count y_count value 2018 a 2 5 109000 2019 a 0 4 nan 2020 a 3 0 nan 2018

我试图为未来几年的资产组建模不同的场景。这是我在Excel中非常乏味地完成的事情,但我想利用我用Pandas构建的大型数据库

例如:

annual_group_cost = 0.02

df1:

year  group  x_count  y_count  value
2018   a       2        5      109000
2019   a       0        4      nan
2020   a       3        0      nan
2018   b       0        0      55000
2019   b       1        0      nan
2020   b       1        0      nan
2018   c       5        1      500000
2019   c       3        0      nan
2020   c       2        5      nan

df2:

group  x_benefit  y_cost  individual_avg  starting_value
a       0.2        0.72     1000            109000
b       0.15       0.75     20000           55000
c       0.15       0.70     20000           500000
我想更新df1中的值,方法是取上一年的值(或起始值)并添加x效益、y成本和年度成本。我假设这需要一个函数来完成,但我不知道有什么有效的方法来处理它

我想要的最终结果是:

df1:

year  group  x_count  y_count  value
2018   a       2        5      103620
2019   a       0        4      98667.3
2020   a       3        0      97294.248
2018   b       0        0      53900
2019   b       1        0      56822
2020   b       1        0      59685.56
2018   c       5        1      495000
2019   c       3        0      497100
2020   c       2        5      420158
我通过以下方式实现了这一点:

starting_value-(starting_value*annual_group_cost)+(x_count*(individual_avg*x_benefit))-(y_count*(individual_avg*y_cost))

由于后续的新值取决于先前计算的新值,因此需要涉及(即使在幕后使用例如
apply
)for循环:

for i in range(1, len(df1)):
    if np.isnan(df1.loc[i, 'value']):
        df1.loc[i, 'value'] = df1.loc[i-1, 'value'] #your logic here

您应该将两个表合并在一起,然后只对数据系列执行函数

hold = df_1.merge(df_2, on=['group']).fillna(0)

x = (hold.x_count*(hold.individual_avg*hold.x_benefit))
y = (hold.y_count*(hold.individual_avg*hold.y_cost))

for year in hold.year.unique():
    start = hold.loc[hold.year == year, 'starting_value']
    hold.loc[hold.year == year, 'value'] = (start-(start*annual_group_cost)+x-y)
    if year != hold.year.max():
       hold.loc[hold.year == year + 1, 'starting_value'] = hold.loc[hold.year == year, 'value'].values

hold.drop(['x_benefit', 'y_cost', 'individual_avg', 'starting_value'],axis=1)
我会给你

   year group  x_count  y_count       value
0  2018     a        2        5    103620.0
1  2019     a        0        4     98667.6
2  2020     a        3        0    97294.25
3  2018     b        0        0     53900.0
4  2019     b        1        0     55822.0
5  2020     b        1        0    57705.56
6  2018     c        5        1    491000.0
7  2019     c        3        0    490180.0
8  2020     c        2        5    416376.4

你的预期产出是什么?我也不确定你的预期产出是什么。但是关于“取上一年的值”,你可以做df['value'].fillna(method='ffill'),我已经用预期输出更新了这个问题。