简体   繁体   中英

rpy2 load multiple DataFrames from a .RData file into pandas

I have an .RData file in the system which contains three objects - all R- DataFrame s. I would like to load them into python as pandas DataFrames. The problem I get is, that the objects are not loaded via rpy , only their names:

from rpy2.robjects import r
from rpy2.robjects import pandas2ri
import pandas as pd
file="./normalization.RData"
rf = r.load(file)
rf

<StrVector - Python:0x7fdf1a28cb08 / R:0x4ce82c8>
['df1', 'df2', 'df3']

f1[2]

'annodf'
In [11]:

Either only the names are loaded, or I do not understand how to access/convert the DFs from the RData.

Does somebody have the solution to this?

Ok - I just realized that the object are not really loaded into rf variable, but into the rpy2.rojbects.r which represents the R-environment.. that is tricky!

Therefore the following works:

import pandas as pd
from rpy2.robjects import r
import rpy2.robjects.pandas2ri as pandas2ri


#load into the env
file="./normalization.RData"
rf=r['load'](file)
rf

<StrVector - Python:0x7fdf1a28cb08 / R:0x4ce82c8>
['df1', 'df2', 'df3']

#acces file in env and convert
df2=pandas2ri.ri2py_dataframe(r['df2'])
type(df2)

pandas.core.frame.DataFrame

For those who get below error:

AttributeError: module 'rpy2.robjects.pandas2ri' has no attribute 'ri2py_dataframe'

change this:

df2 = pandas2ri.ri2py_dataframe(r['df2'])

to this:

df2 = rpy2.robjects.conversion.rpy2py(r['df2'])

Used R version: 3.6.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM