How can I import a data frame from R saved as RData to pandas?

Viewed 2418

I'm trying to import a data frame from R saved as RData to a pandas data frame. How can I do so? I unsuccessfully tried using rpy2 as follows:

import pandas as pd
from rpy2.robjects import r
from rpy2.robjects import pandas2ri
pandas2ri.activate()

# I use iris for convenience but I could have done r.load('my_data.RData')
print(r.data('iris'))
print(r['iris'].head())
print(type(r.data('iris')))

print(pandas2ri.ri2py_dataframe(r.data('iris')))
print(pandas2ri.ri2py(r.data('iris')))
print(pd.DataFrame(r.data('iris')))

outputs:

[1] "iris"

   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
1           5.1          3.5           1.4          0.2  setosa
2           4.9          3.0           1.4          0.2  setosa
3           4.7          3.2           1.3          0.2  setosa
4           4.6          3.1           1.5          0.2  setosa
5           5.0          3.6           1.4          0.2  setosa
<class 'rpy2.robjects.vectors.StrVector'>
   0  1  2  3
0  i  r  i  s
['iris']

I use pandas 0.20.1 + python 3.6 x64 + Windows 7.

1 Answers
Related