将数据从 R 网化到 python 并再次返回到 R
Reticulate data from R to python and back to R again
我哪里错了?我在 RStudio 中,我想对 Python 中的一些文本数据进行一些处理,然后将其带回 R 进行最终分析/绘图,但出现错误:
NameError: name 'df_py' is not defined
数据与代码:
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
一旦我们进入 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)
我哪里错了?我在 RStudio 中,我想对 Python 中的一些文本数据进行一些处理,然后将其带回 R 进行最终分析/绘图,但出现错误:
NameError: name 'df_py' is not defined
数据与代码:
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
一旦我们进入 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)