嵌入式 R 尝试添加循环

Embedded R Try Except Add Loop

作为 python/R 新手,我正在关注下面的博客,但在向下面的代码添加循环语句时遇到了问题。目前我能够获得完整的代码 运行,但只能为 1 位客户输出季节性标志。我希望它为我的所有客户循环播放 运行。

datamovesme.com/2018/07/01/seasonality-python-code

##Here comes the R code piece     
     try:
          seasonal = r(''' 
          fit<-tbats(customerTS, seasonal.periods = 12, use.parallel = TRUE)
          fit$seasonal
          ''')
      except: seasonal = 1
      seasonal_output = seasonal_output.append({'customer_id':customerid, 'seasonal': seasonal}, ignore_index=True)
      print(f' {customerid} | {seasonal} ')
print(seasonal_output)
seasonal_output.to_csv(outfile)

我已经尝试了很多代码组合来让它循环,太多了不能在这里列出。该博客展示了我们可用的现有数据框和时间序列对象。我不确定使用哪一个以及如何将它传递给 R 代码。

谢谢!

博客 link 维护问题:

  1. 代码没有按照 Python 语法中的要求正确缩进行。这可能是由于网站呈现白色 space 或制表符,但这对读者来说是一种伤害,因为缺少缩进更改输出。

  2. 代码未能注意到附加数据帧的低效率问题:。相反,由于 seasonal 是一个值,因此构建了一个字典列表,您可以在循环外将其放入 pd.DataFrame() 构造函数中。

解决上述问题和 运行 整个代码块后,您的解决方案应该输出一个跨越所有 customerids.

的数据帧
# ... same above assignments ...
outfile = '[put your file path here].csv'
df_list = []

for customerid, dataForCustomer in filledIn.groupby(by=['customer_id']):
    startYear = dataForCustomer.head(1).iloc[0].yr
    startMonth = dataForCustomer.head(1).iloc[0].mnth
    endYear = dataForCustomer.tail(1).iloc[0].yr
    endMonth = dataForCustomer.tail(1).iloc[0].mnth

    #Creating a time series object
    customerTS = stats.ts(dataForCustomer.usage.astype(int),
                          start=base.c(startYear,startMonth),
                          end=base.c(endYear, endMonth), 
                          frequency=12)
    r.assign('customerTS', customerTS)

    ##Here comes the R code piece
    try:
        seasonal = r('''
                        fit<-tbats(customerTS, seasonal.periods = 12, use.parallel = TRUE)
                        fit$seasonal
                     ''')
    except: 
        seasonal = 1

    # APPEND DICTIONARY TO LIST (NOT DATA FRAME)
    df_list.append({'customer_id': customerid, 'seasonal': seasonal})
    print(f' {customerid} | {seasonal} ')

seasonal_output = pd.DataFrame(df_list)
print(seasonal_output)
seasonal_output.to_csv(outfile)