Converting DataFrames to and from R

from pandas import DataFrame

from rpy2.robjects import pandas2ri
pandas2ri.activate()

Create a pandas DataFrame.

df = DataFrame({'a': [8, 21, 23], 'b': [7, 8, 4], 'c': [400, 83, 98]},
               index=["one", "two", "three"])
df
a b c
one 8 7 400
two 21 8 83
three 23 4 98

Explicitly convert into an R data frame.

r_df = pandas2ri.py2ri(df)
type(r_df)
rpy2.robjects.vectors.DataFrame

Explicitly convert back into a pandas DataFrame.

df = pandas2ri.ri2py(r_df)
df
a b c
one 8 7 400
two 21 8 83
three 23 4 98

If you spot any errors please let me know on twitter.