Python DataFrame中的嵌套循环可创建多个时间序列预测

问题描述 投票:-2回答:1

我是Python的新手,主要需要Stats使用。这是数据集的一瞥:

Code    City    Date    Sales   
K1  W   1/1/2017    46506.92  
K1  X   1/1/2017    187195.2  
K1  Y   1/1/2017    12858.15  
K1  Z   1/1/2017    25300.88  
K2  W   1/1/2017    87731.47  
K2  X   1/1/2017    14952.8  
K3  Y   1/1/2017    167.8204  
K4  A   1/1/2017    9602.108  
K4  B   1/1/2017    16034.13  
K4  C   1/1/2017    106.5196  
K4  D   1/1/2017    1057.269  
K5  W   1/1/2017    12346.57  
K5  X   1/1/2017    528776.5  
K5  Y   1/1/2017    7598.979  
K5  Z   1/1/2017    147969.6  
K6  W   1/1/2017    11770.68  
K6  X   1/1/2017    180867.6  
K6  Y   1/1/2017    11778.6  
K6  Z   1/1/2017    48835.3  

City =字符串列表和相同的代码可能位于多个城市,但是每个Code-City组合都是唯一的,具有32个数据点。数据有效期为32个月,每个月1日收集一次。我需要根据各个预测创建一个rmse错误值数组。每个预测都是代码城市级别的。我为ARIMA写了一个def函数(不能使用先知权变),并试图编写两个for循环,分别在“ Code”和“ City”列表上进行迭代。无法找出逻辑为

for i in [list_of_unique_codes]:  
      for i in [list_of_unique_City]:  
        ##I know I need an if statement somewhere which checks if Code-City combination exists but can not come up with the logic. For example, if Code exists in list of codes, move over to second for loop. Check if the City is present for that Code, if yes, call the defined function for ARIMA. The reason being same code exists in multiple cities.   

我想将结果的均方根值-实际值存储在数组中,并在每次迭代后继续附加。我期望使用ARIMA预测的预测输出的5个浮点值的数组。

python pandas for-loop time-series iteration
1个回答
0
投票

要进行单独的预测,您只能预先获取数据中存在的代码和城市组合,而不能尝试所有组合。

for code, city in df[['Code', 'City']].drop_duplicates().values:
    train_df = df[(df['Code']==code)&(df['City']==city)].sort_values(by='Date')
    .....

© www.soinside.com 2019 - 2024. All rights reserved.