我在哪里错了?我在RStudio中,我想对Python中的某些文本数据进行一些处理,然后将其带回R进行最终分析/绘图,但出现错误:
NameError:名称'df_py'未定义
数据和代码:
text <- c("Because I could not stop for Death -",
"He kindly stopped for me -",
"The Carriage held but just Ourselves -",
"and Immortality")
ID <- c(1,2,3,4)
df <- data.frame(cbind(ID, text))
library(reticulate)
df_py <- r_to_py(df)
repl_python()
df_py_clean['text'] = df_py['text'].str.replace("[^a-zA-Z]", " ")
df_py_clean['text'] = df_py['text'].str.lower()
exit
答案 0 :(得分:4)
一旦我们进入python
REPL
,请使用r.
访问对象
library(reticulate)
df_py <- r_to_py(df)
repl_python()
>>> r.df_py
# ID text
#0 1 Because I could not stop for Death -
#1 2 He kindly stopped for me -
#2 3 The Carriage held but just Ourselves -
#3 4 and Immortality
现在进行转换
>>> r.df_py['text'] = r.df_py['text'].str.replace("[^a-zA-Z]", " ")
>>> r.df_py['text'] = r.df_py['text'].str.lower()
>>> exit
从R
df_py
# ID text
#0 1 because i could not stop for death
#1 2 he kindly stopped for me
#2 3 the carriage held but just ourselves
#3 4 and immortality
注意:创建df_py_clean
对象时不清楚。因此,相反,我们在这里从python更新同一对象列
注2:从R
环境访问python对象的反向操作是py$
text <- c("Because I could not stop for Death -",
"He kindly stopped for me -",
"The Carriage held but just Ourselves -",
"and Immortality")
ID <- c(1,2,3,4)
df <- data.frame(ID, text)