首页 文章

如何将Python Pandas Dataframe转换为R data.frame

提问于
浏览
2

我正在使用R包 PythonInR ,并希望使用函数 pyGet() 将pandas数据帧转换为R数据帧,但我收到以下错误:

as.data.frame.default中的错误(xi,optional = TRUE,stringsAsFactors = stringsAsFactors):无法将类“”PythonObject“”强制转换为data.frame

text.csv文件只包含两列,name(string)和value(int) . 代码如下:

library(PythonInR) 
pyConnect() 
pyIsConnected() 
pyVersion()
pyOptions("usePandas", TRUE) 
pyImport("pandas", as="pd") 
test_code <-'py_df = pd.read_csv("test.csv")' 
pyExec(code = test_code) 
r_df <- pyGet("py_df")

并且代码的输出是:

library(PythonInR)
pyConnect()
R is already connected to Python!
pyIsConnected()
[1] TRUE
pyVersion()
[1] "3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]"
pyOptions("usePandas", TRUE)
pyImport("pandas", as="pd")
test_code <- 'py_df = pd.read_csv("test.csv")'
pyExec(code = test_code)
r_df <- pyGet("py_df")

as.data.frame.default(x [[i]],optional = TRUE,stringsAsFactors = stringsAsFactors)中的错误:无法将类“”PythonObject“”强制转换为data.frame

任何人都可以帮助建议如何使用 PythonInR 将Pandas数据帧转换为R data.frame?

2 回答

  • 1

    你可以使用json . pandas数据帧具有 to_json 方法,您可以使用 rjson R包来使用 fromJSON 读取json字符串 .

    您可能还会发现thisthis很有趣 . 计划很快提交给CRAN,但我需要做更多的测试 .

  • 0

    很难说它为什么不起作用 .

    你有一个可重复的例子,或者你能提供“test.csv”吗?如果您认为这是一个错误,您可以给包裹作者写一封邮件 .

    到目前为止,我看到,

    sessionInfo()
    
    ## R version 3.4.0 (2017-04-21)
    ## Platform: x86_64-pc-linux-gnu (64-bit)
    ## Running under: Debian GNU/Linux 8 (jessie)
    ## 
    ## Matrix products: default
    ## BLAS: /home/florian/bin/R_dev/lib/libRblas.so
    ## LAPACK: /home/florian/bin/R_dev/lib/libRlapack.so
    ## 
    ## locale:
    ##  [1] LC_CTYPE=de_AT.UTF-8       LC_NUMERIC=C              
    ##  [3] LC_TIME=de_AT.UTF-8        LC_COLLATE=de_AT.UTF-8    
    ##  [5] LC_MONETARY=de_AT.UTF-8    LC_MESSAGES=de_AT.UTF-8   
    ##  [7] LC_PAPER=de_AT.UTF-8       LC_NAME=C                 
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    ## [11] LC_MEASUREMENT=de_AT.UTF-8 LC_IDENTIFICATION=C       
    ## 
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base     
    ## 
    ## other attached packages:
    ## [1] knitr_1.16      PythonInR_0.1-3
    ## 
    ## loaded via a namespace (and not attached):
    ## [1] compiler_3.4.0 magrittr_1.5   R6_2.2.1       markdown_0.8  
    ## [5] tools_3.4.0    stringi_1.1.5  pack_0.1-1     stringr_1.2.0 
    ## [9] evaluate_0.10
    

    _

    library(PythonInR)
    pyConnect() 
    ## R is already connected to Python!
    

    _

    pyIsConnected() 
    ## [1] TRUE
    

    _

    pyVersion()
    ## [1] "2.7.9 (default, Mar  1 2015, 13:01:26) \n[GCC 4.9.2]"
    

    _

    pyOptions("usePandas", TRUE) 
    pyImport("pandas", as="pd")
    

    _

    pySet("x", head(cars))   
    pyExecp("x")
    ##    dist  speed
    ## 0     2      4
    ## 1    10      4
    ## 2     4      7
    ## 3    22      7
    ## 4    16      8
    ## 5    10      9
    

    _

    pyExecp("type(x)")
    ## <class 'pandas.core.frame.DataFrame'>
    str(pyGet("x"))
    ## 'data.frame':    6 obs. of  2 variables:
    ##  $ speed: num  4 4 7 7 8 9
    ##  $ dist : num  2 10 4 22 16 10
    

相关问题