将数据从R传递到python,然后再次返回R

时间:2019-07-14 14:55:09

标签: python r reticulate

我在哪里错了?我在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

1 个答案:

答案 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)
相关问题