Pandas - how to convert r dataframe back to pandas? Pandas - how to convert r dataframe back to pandas? r r

Pandas - how to convert r dataframe back to pandas?


Since rpy2 release 2.4.0 converting data frames back and forth between rpy2 and pandas is included as an optional module. With it, no need to convert explicitly, it will be done on the fly.

The documentation contains examples (also available as a Jupyter notebook - link available near the top of the page): https://rpy2.github.io/doc/latest/html/pandas.html#interoperability-with-pandas

Note: The original answer to this question recommended the following.

from rpy2.robjects import pandas2ripandas2ri.activate()

If wishing to convert explicitly for any reason, the functions are pandas2ri.py2ri() and pandas2ri.ri2py() (they were pandas2ri.pandas2ri() and pandas2ri.ri2pandas()).

Note: Since rpy2 release 3.3.0 explicit conversion is done as follows

import rpy2.robjects as rodt = pd.DataFrame()# To R DataFramer_dt = ro.conversion.py2rpy(dt)# To pandas DataFramepd_dt = ro.conversion.rpy2py(r_dt)

For more details check out this link.


As suggested by lgautier, it can be done with pandas2ri.

Here is sample code for convert rpy dataframe (rdf) to pandas dataframe (pd_df):

from rpy2.robjects import pandas2ripd_df = pandas2ri.ri2py_dataframe(rdf)


Given your import, it appears it is:

com.convert_robj(rdf)

For example,

In [480]: dfrmOut[480]:           A          B  C0   0.454459  49.916767  11   0.943284  50.878174  12   0.974856  50.335679  23   0.776600  50.782104  14   0.553895  50.084505  15   0.514018  50.719019  26   0.915413  50.513962  07   0.771571  49.859855  28   0.068619  49.409657  09   0.728141  50.945174  210  0.388115  47.879653  111  0.960172  49.680258  012  0.015216  50.067968  013  0.495024  50.286287  114  0.565954  49.909771  115  0.992279  49.009696  116  0.179934  49.554256  017  0.521243  47.854791  018  0.551241  51.076262  119  0.713271  49.418503  020  0.801716  50.660304  1In [481]: rdfrm = com.convert_to_r_dataframe(dfrm)In [482]: rdfrmOut[482]:<DataFrame - Python:0x14905cf8 / R:0x1600ee98>[FloatVector, FloatVector, IntVector]  A: <class 'rpy2.robjects.vectors.FloatVector'>  <FloatVector - Python:0xf9d0b00 / R:0x140e2620>[0.454459, 0.943284, 0.974856, ..., 0.551241, 0.713271, 0.801716]  B: <class 'rpy2.robjects.vectors.FloatVector'>  <FloatVector - Python:0xf9d0878 / R:0x125aa240>[49.916767, 50.878174, 50.335679, ..., 51.076262, 49.418503, 50.660304]  C: <class 'rpy2.robjects.vectors.IntVector'>  <IntVector - Python:0x11fceef0 / R:0x13f0d918>[       1,        1,        2, ...,        1,        0,        1]In [483]: com.convert_robj(rdfrm)Out[483]:           A          B  C0   0.454459  49.916767  11   0.943284  50.878174  12   0.974856  50.335679  23   0.776600  50.782104  14   0.553895  50.084505  15   0.514018  50.719019  26   0.915413  50.513962  07   0.771571  49.859855  28   0.068619  49.409657  09   0.728141  50.945174  210  0.388115  47.879653  111  0.960172  49.680258  012  0.015216  50.067968  013  0.495024  50.286287  114  0.565954  49.909771  115  0.992279  49.009696  116  0.179934  49.554256  017  0.521243  47.854791  018  0.551241  51.076262  119  0.713271  49.418503  020  0.801716  50.660304  1

With docs:

In [475]: com.convert_robj?Type:       functionString Form:<function convert_robj at 0x13e85848>File:       /mnt/epd/7.3-2_pandas0.12/lib/python2.7/site-packages/pandas/rpy/common.pyDefinition: com.convert_robj(obj, use_pandas=True)Docstring:Convert rpy2 object to a pandas-friendly formParameters----------obj : rpy2 objectReturns-------Non-rpy data structure, mix of NumPy and pandas objects