Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - how to convert r dataframe back to pandas?

Tags:

python

pandas

r

I converted a pandas df to r using the the below:

import pandas as pd import pandas.rpy.common as com import rpy2.robjects as ro from rpy2.robjects.packages import importr rdf = com.convert_to_r_dataframe(df) 

How do I convert rdf back to a pandas df?

df = f(rdf) ? 
like image 741
Tampa Avatar asked Dec 17 '13 09:12

Tampa


People also ask

How do you convert DataFrame to pandas DataFrame?

Convert PySpark Dataframe to Pandas DataFramePySpark DataFrame provides a method toPandas() to convert it to Python Pandas DataFrame. toPandas() results in the collection of all records in the PySpark DataFrame to the driver program and should be done only on a small subset of the data.

How do you undo changes in pandas?

CTRL + Z is the only thing that I can think of.

How do I convert DF to pandas CSV?

By using pandas. DataFrame. to_csv() method you can write/save/export a pandas DataFrame to CSV File. By default to_csv() method export DataFrame to a CSV file with comma delimiter and row index as the first column.

How do I change to INT panda?

Convert Column to int (Integer)Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.


2 Answers

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 pandas2ri pandas2ri.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 ro  dt = pd.DataFrame() # To R DataFrame r_dt = ro.conversion.py2rpy(dt) # To pandas DataFrame pd_dt = ro.conversion.rpy2py(r_dt) 

For more details check out this link.

like image 149
lgautier Avatar answered Sep 21 '22 04:09

lgautier


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 pandas2ri  pd_df = pandas2ri.ri2py_dataframe(rdf) 
like image 43
huojun Avatar answered Sep 18 '22 04:09

huojun