Reputation: 7107
Where am I going wrong here? I am in RStudio and I want to do some processing on some text data in Python and bring it back to R for some final analysis / plots but I get the error:
NameError: name 'df_py' is not defined
Data & Code:
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
Upvotes: 5
Views: 3882
Reputation: 887231
Once we are in the python
REPL
, use r.
to access the object
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
and now do the transformation
>>> r.df_py['text'] = r.df_py['text'].str.replace("[^a-zA-Z]", " ")
>>> r.df_py['text'] = r.df_py['text'].str.lower()
>>> exit
call the object from 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
NOTE: Not clear when the df_py_clean
object was created. So, instead of that, here we are updating the same object column from python
NOTE2: The reverse to access python objects from R
environment would be 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)
Upvotes: 11