Azure Databricks Jupyter Notebook Python和R在一个单元格中



我有一些代码(大部分不是我的原始代码(,我在本地PC Anaconda Jupyter Notebook环境中运行。我需要扩大处理规模,所以我正在研究Azure Databricks。有一段代码运行Python循环,但使用R库(stats(,然后通过R模型(tbats(传递数据。所以Jupyter Notebook的一个单元运行python和R代码。这也可以在Azure Databricks笔记本中完成吗?我只找到了一些文档,可以让你在不同的单元格中更改语言。

在以前的单元格中,我有:

%r libarary(stats) 

因此,将导入库统计信息(以及其他R库(。然而,当我运行下面的代码时,我得到了

NameError:名称"stats"未定义

我想知道Databricks是否希望你告诉单元格你正在使用的语言(例如%r、%python等(。

我的Python代码:

for customerid, dataForCustomer in original.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 = pa.DataFrame(df_list)

如果您更改数据块中的语言,您将无法获得上一种语言的变量

相关内容

最新更新