簡體   English   中英

將數據從R網格化為python並再次返回到R.

[英]Reticulate data from R to python and back to R again

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

一旦我們在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM