0

I am trying to load a SPSS file into a Pandas DataFrame in Python, and am looking for easier ways to do it from more recent developments in using R codes in the Python environment, which lead me to PyRserve.

After connecting to PyRserve,

import pyRserve
conn = pyRserve.connect()

One can pretty much run basic r codes such as

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

However, if possible in PyRserve, how do one import an R library to load a dataframe with r codes like the ones below,

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

and hopefully onto a pandas DataFrame? Any thoughts are appreciated!

Carl H
  • 1,036
  • 2
  • 15
  • 27

1 Answers1

-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()

My apologies for not explaining it properly... I am adding my github link with it so you can see more examples . I think I have explained it properly there https://github.com/shintojoseph1234/Rserve-and-pyRserve

Shinto Joseph
  • 2,809
  • 27
  • 25