如何在列中为每个值附加时间表?

时间:2019-07-26 18:18:15

标签: python

我正在尝试预测一系列不同对象的消耗率,直到它们达到0。

说我有10个模型。 A B C D E F G H I J。

A应该附加了从2019年到2100年的年份。复制行中的值超过81次。

那么我想基本上说2020年,前几年的预测库存量是多少?我将使用上一行的数字来计算2020年的广告资源,然后在2021年回溯到2020年。等等。

目前,我只能通过每个指标每年创建一列来做到这一点,这是不理想的。

我想进行此计算,这样就不必在下一年连续添加新列。而是我想添加行

build_plans = ['2019 build plan',
   '2020 build plan', '2021 build plan', '2022 build plan',
   '2023 build plan', '2024 build plan', '2025 build plan',
   '2026 build plan', '2027 build plan', '2028 build plan',
   '2029 build plan', '2030 build plan', '2031 build plan',
   '2032 build plan', '2033 build plan', '2034 build plan',
   '2035 build plan', '2036 build plan', '2037 build plan',
   '2038 build plan', '2039 build plan', '2040 build plan',
   '2041 build plan', '2042 build plan', '2043 build plan',
   '2044 build plan', '2045 build plan', '2046 build plan',
   '2047 build plan', '2048 build plan', '2049 build plan',
   '2050 build plan', '2051 build plan', '2052 build plan',
   '2053 build plan', '2054 build plan', '2055 build plan',
   '2056 build plan', '2057 build plan', '2058 build plan',
   '2059 build plan', '2060 build plan', '2061 build plan',
   '2062 build plan', '2063 build plan', '2064 build plan',
   '2065 build plan', '2066 build plan', '2067 build plan',
   '2068 build plan', '2069 build plan', '2070 build plan',
   '2071 build plan', '2072 build plan', '2073 build plan',
   '2074 build plan', '2075 build plan', '2076 build plan',
   '2077 build plan', '2078 build plan', '2079 build plan',
   '2080 build plan', '2081 build plan', '2082 build plan',
   '2083 build plan', '2084 build plan', '2085 build plan',
   '2086 build plan', '2087 build plan', '2088 build plan',
   '2089 build plan', '2090 build plan', '2091 build plan',
   '2092 build plan', '2093 build plan', '2094 build plan',
   '2095 build plan', '2096 build plan', '2097 build plan',
   '2098 build plan', '2099 build plan', '2100 build plan']





 i=0

 for x in build_plans:

    t = x[:5]
    y=0

    if i==0:
        LTB[f"{t} Install Base Forecasted "] = LTB["Install Base"] + LTB[x]
        LTB[f"{t} Components used in new builds"] =  LTB[x]
        LTB[f"{t} Components used in Z"] =  LTB[x] * LTB[Z Replacements']
        LTB[f"{t} Components used in B"] =  LTB[f"{t} Install Base Forecasted "]  * LTB['B Replacements']
        LTB[f"{t} Total Components Used "] = LTB[f"{t} Components used in new builds"] +LTB[f"{t} Components used in Warranty"]+ LTB[f"{t} Components used in B"]
        LTB[f"{t} Account for __ yield "] =  LTB[f"{t} Total Components Used "] /.99/.98
        LTB[f"{t} buffer 10%"] =  LTB[f"{t} Account for__ yield "] *1.1
        LTB[f"{t} Forecasted Inventory "] = LTB[f"{t} Install Base Forecasted "] -  LTB[f"{t} buffer 10%"]


    else: 
        z = build_plans[i-1]
        v= z[:5]

        LTB[f"{t} Install Base Forecasted "] = LTB[f"{v} Install Base Forecasted "] + LTB[x] -LTB[f"{v} Components used in B"]
        LTB[f"{t} Components used in new builds"] =  LTB[x]
        LTB[f"{t} Components used in Z"] =  LTB[x] * LTB[Z Replacements']
        LTB[f"{t} Components used in B"] =  LTB[f"{t} Install Base Forecasted "]  * LTB['B Replacements']
        LTB[f"{t} Total Components Used "] = LTB[f"{t} Components used in new builds"] +LTB[f"{t} Components used in Warranty"]+ LTB[f"{t} Components used in B"]
        LTB[f"{t} Account for __ yield "] =  LTB[f"{t} Total Components Used "] /.99/.98
        LTB[f"{t} buffer 10%"] =  LTB[f"{t} Account for__ yield "] *1.1
        LTB[f"{t} Forecasted Inventory "] = LTB[f"{t} Install Base Forecasted "] -  LTB[f"{t} buffer 10%"]

        LTB[f"{t} Install Base Forecasted "] = LTB.loc[LTB[f"{t} Install Base Forecasted "]<=0, f"{t} Install Base Forecasted "] =0
        LTB[f"{t} Forecasted Inventory "] = LTB.loc[LTB[f"{t} Forecasted Inventory "]<=0, f"{t} Forecasted Inventory "] =0
    i+=1

`

我不想创建这些列,我想要一个年份值从2019到2100的列,以及每个与当前年份相对应的输出的列

1 个答案:

答案 0 :(得分:0)

我已经使用临时DF解决了该问题:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1,2], [2,1]]), columns = ["A", "B"])
year = pd.Series(range(2019,2100,1))
index = len(year)* len(df)
index
sd= pd.DataFrame(index = range(0, index+1,1))
i=0
for x in year:
    for z,y in df.iterrows():
        sd.loc[i,"Year"] =x
        sd.loc[i,"Value"] =df.loc[z,"A"]
        sd.loc[i,"Value2"] =df.loc[z,"B"]
        i+=1

结果:

    Year  Value     Value2
0   2019.0  1.0     2.0
1   2019.0  2.0     1.0
2   2020.0  1.0     2.0
3   2020.0  2.0     1.0
4   2021.0  1.0     2.0
5   2021.0  2.0     1.0
6   2022.0  1.0     2.0
7   2022.0  2.0     1.0
8   2023.0  1.0     2.0
9   2023.0  2.0     1.0
10  2024.0  1.0     2.0
...     ...     ...     ...


163 rows × 3 columns

这是我要解决的主要问题。如何获取数据框并根据年份范围进行扩展。这段代码似乎可以解决它。

要引入上一年的值,循环的主体内将是这样的:

index = len(year)* len(df)
index
sd= pd.DataFrame(index = range(0, index+1,1))
i=0
for x in year:
    for z,y in df.iterrows():
        sd.loc[i,"Year"] =x
        if i!=0:
            sd.loc[i,"Value"] =sd.loc[i-1,"Value"] + df.loc[z,"A"]
        else:
            sd.loc[i,"Value"] =df.loc[z,"A"]
        sd.loc[i,"Value2"] =df.loc[z,"B"]
        i+=1


    Year    Value   Value2
0   2019.0  1.0     2.0
1   2019.0  3.0     1.0
2   2020.0  4.0     2.0
3   2020.0  6.0     1.0
4   2021.0  7.0     2.0
5   2021.0  9.0     1.0
6   2022.0  10.0    2.0
7   2022.0  12.0    1.0
8   2023.0  13.0    2.0
9   2023.0  15.0    1.0