Generate some data in Python:


In [1]:
import pandas as pd
import numpy as np

In [2]:
# Generate matrix
my_df = np.random.random((3, 3))
my_df


Out[2]:
array([[ 0.09127492,  0.91767262,  0.04072103],
       [ 0.14016312,  0.71447439,  0.16819969],
       [ 0.35681263,  0.41574813,  0.71361618]])

Enable use of R:


In [3]:
%load_ext rpy2.ipython

Use R to manipulate the Python data:


In [4]:
%%R -i my_df
# "-i" is to load Python variable into R

my_df


           [,1]      [,2]       [,3]
[1,] 0.09127492 0.9176726 0.04072103
[2,] 0.14016312 0.7144744 0.16819969
[3,] 0.35681263 0.4157481 0.71361618

In [5]:
%%R -o my_rowsum
# "-o" is export variable from R to Python

# Sum over rows
(my_rowsum <- apply(my_df, 1, sum))


[1] 1.049669 1.022837 1.486177

Back to Python:


In [6]:
my_rowsum


Out[6]:
array([ 1.04966857,  1.02283719,  1.48617694])