2016-09-18 42 views
0

我试图加载SPSS文件来调用库(国外)成熊猫数据帧在Python,并正在寻找更容易的方式做到这一点从最近的事态发展在Python环境,使用R代码,这导致我到PyRserve。使用PyRserve在Python

连接到PyRserve后,

import pyRserve 
conn = pyRserve.connect() 

一个几乎可以运行基础研发代码,例如

conn.eval('3+5') #output = 8.0 

然而,如果可能的话在PyRserve,怎么做一个导入的R库加载如下所示的r代码的数据帧,

library(foreign) 
dat<-read.spss("/path/spss_file.sav", to.data.frame=TRUE) 

并希望到一个熊猫DataFrame?任何想法都很感激!

回答

1
#import pyRserve 
import pyRserve 

#open pyRserve connection 
conn = pyRserve.connect() 

#load your rscript into a variable (you can even write functions) 
test_r_script = ''' 
       library(foreign) 
       dat<-read.spss("/path/spss_file.sav", 
           to.data.frame=TRUE) 
       ''' 

#do the connection eval 
variable = conn.eval(test_r_script) 

print variable 

# closing the pyRserve connection 
conn.close() 

我很抱歉没有正确解释它......我加了我的github链接,所以你可以看到更多的例子。我想我已经有正确 https://github.com/shintojoseph1234/Rserve-and-pyRserve

+0

只需经过README.md并且还可以通过views.py解释它。我已经为python/Django项目创建了它。 –