R interface — pandas 0.20.3 documentation (original) (raw)
rpy2 / R interface¶
Warning
Up to pandas 0.19, a pandas.rpy
module existed with functionality to convert between pandas and rpy2
objects. This functionality now lives in the rpy2 project itself. See the updating sectionof the previous documentation for a guide to port your code from the removed pandas.rpy
to rpy2
functions.
rpy2 is an interface to R running embedded in a Python process, and also includes functionality to deal with pandas DataFrames. Converting data frames back and forth between rpy2 and pandas should be largely automated (no need to convert explicitly, it will be done on the fly in most rpy2 functions). To convert explicitly, the functions are pandas2ri.py2ri()
andpandas2ri.ri2py()
.
See also the documentation of the rpy2 project: https://rpy2.readthedocs.io.
In the remainder of this page, a few examples of explicit conversion is given. The pandas conversion of rpy2 needs first to be activated:
In [1]: from rpy2.robjects import r, pandas2ri
In [2]: pandas2ri.activate()
Transferring R data sets into Python¶
Once the pandas conversion is activated (pandas2ri.activate()
), many conversions of R to pandas objects will be done automatically. For example, to obtain the ‘iris’ dataset as a pandas DataFrame:
In [3]: r.data('iris') Out[3]: R object with classes: ('character',) mapped to: <StrVector - Python:0x137224388 / R:0x7fd4d4f7b118> ['iris']
In [4]: r['iris'].head() Out[4]: 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
If the pandas conversion was not activated, the above could also be accomplished by explicitly converting it with the pandas2ri.ri2py
function (pandas2ri.ri2py(r['iris'])
).
Converting DataFrames into R objects¶
The pandas2ri.py2ri
function support the reverse operation to convert DataFrames into the equivalent R object (that is, data.frame):
In [5]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]}, ...: index=["one", "two", "three"]) ...:
In [6]: r_dataframe = pandas2ri.py2ri(df)
In [7]: print(type(r_dataframe)) <class 'rpy2.robjects.vectors.DataFrame'>
In [8]: print(r_dataframe) A B C one 1 4 7 two 2 5 8 three 3 6 9
The DataFrame’s index is stored as the rownames
attribute of the data.frame instance.