Skip to content

Commit 996d529

Browse files
committed
Review datapythonista for file r_interface.rst
Signed-off-by: Fabian Haase <[email protected]>
1 parent 659d55c commit 996d529

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

doc/source/r_interface.rst

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,27 @@ See also the documentation of the `rpy2 <http://rpy2.bitbucket.org/>`__ project:
3333

3434
In the remainder of this page, a few examples of explicit conversion is given. The pandas conversion of rpy2 needs first to be activated:
3535

36-
.. code-block:: python
36+
.. ipython:: ipython
37+
:verbatim:
3738

38-
>>> from rpy2.robjects import pandas2ri
39-
>>> pandas2ri.activate()
39+
In [1]: from rpy2.robjects import pandas2ri
40+
...: pandas2ri.activate()
4041

4142
Transferring R data sets into Python
4243
------------------------------------
4344

4445
Once the pandas conversion is activated (``pandas2ri.activate()``), many conversions
4546
of R to pandas objects will be done automatically. For example, to obtain the 'iris' dataset as a pandas DataFrame:
4647

47-
.. code-block:: python
48+
.. ipython:: ipython
49+
:verbatim:
4850

49-
>>> from rpy2.robjects import r
50-
>>> r.data('iris')
51-
>>> r['iris'].head()
51+
In [2]: from rpy2.robjects import r
52+
53+
In [3]: r.data('iris')
54+
55+
In [4]: r['iris'].head()
56+
Out[4]:
5257
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5358
0 5.1 3.5 1.4 0.2 setosa
5459
1 4.9 3.0 1.4 0.2 setosa
@@ -66,19 +71,25 @@ Converting DataFrames into R objects
6671
The ``pandas2ri.py2ri`` function support the reverse operation to convert
6772
DataFrames into the equivalent R object (that is, **data.frame**):
6873

69-
.. code-block:: python
74+
.. ipython:: ipython
75+
:verbatim:
76+
77+
In [5]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
78+
...: index=["one", "two", "three"])
7079

71-
>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
72-
... index=["one", "two", "three"])
73-
>>> r_dataframe = pandas2ri.py2ri(df)
74-
>>> print(type(r_dataframe))
75-
<class 'rpy2.robjects.vectors.DataFrame'>
76-
>>> print(r_dataframe)
80+
In [6]: r_dataframe = pandas2ri.py2ri(df)
81+
82+
In [7]: print(type(r_dataframe))
83+
Out[7]: <class 'rpy2.robjects.vectors.DataFrame'>
84+
85+
In [8]: print(r_dataframe)
86+
Out[8]:
7787
A B C
7888
one 1 4 7
7989
two 2 5 8
8090
three 3 6 9
8191

92+
8293
The DataFrame's index is stored as the ``rownames`` attribute of the
8394
data.frame instance.
8495

0 commit comments

Comments
 (0)